diff --git a/src/runtime/composables/useGtag.ts b/src/runtime/composables/useGtag.ts index 17e2fd1..a5a8f0f 100644 --- a/src/runtime/composables/useGtag.ts +++ b/src/runtime/composables/useGtag.ts @@ -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': '', + }], + }) + } } } diff --git a/src/runtime/plugin.client.ts b/src/runtime/plugin.client.ts index 00fe0b9..00880e3 100644 --- a/src/runtime/plugin.client.ts +++ b/src/runtime/plugin.client.ts @@ -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': '', }, ], })