Skip to content

Commit

Permalink
fix: ensure head script is injected once
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Mar 1, 2024
1 parent 4542e64 commit 2eb26fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/runtime/composables/useGtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ export function useGtag() {
if (!window.dataLayer)
initGtag({ tags })

// If the `dataLayer` has more than two items
// it is considered to be initialized.
if (window.dataLayer!.length > 2)
return

// Inject the Google tag script.
useHead({
script: [{ src: withQuery(options.url, { id: tag.id }) }],
})
// Inject the Google tag script if it wasn't injected by the client plugin.
if (!document.head.querySelector('script[data-gtag]')) {
useHead({
script: [{
'src': withQuery(options.url, { id: tag.id }),
'data-gtag': '',
}],
})
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export default defineNuxtPlugin({
useHead({
script: [
{
src: withQuery(options.url, { id: tags[0].id }),
'src': withQuery(options.url, { id: tags[0].id }),
[strategy]: true,
'data-gtag': '',
},
],
})
Expand Down

0 comments on commit 2eb26fe

Please sign in to comment.