From c55f630a38065afe8f47e2440d560d258dd00b8a Mon Sep 17 00:00:00 2001 From: Jon McClure Date: Fri, 18 Nov 2022 17:21:35 +0000 Subject: [PATCH] better handling for gpt --- src/components/SEO/SEO.svelte | 6 +----- src/components/SEO/publisherTags.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/components/SEO/SEO.svelte b/src/components/SEO/SEO.svelte index ab2974e6..de7134f5 100644 --- a/src/components/SEO/SEO.svelte +++ b/src/components/SEO/SEO.svelte @@ -91,15 +91,11 @@ $: origin = getOrigin(baseUrl); $: canonicalUrl = origin + pageUrl.pathname; - let hasEnabledPublisherTags = false; // Only fire analytics on prod sites $: { if (typeof window !== 'undefined' && includeAnalytics) { analytics(canonicalUrl, seoTitle); - if (!hasEnabledPublisherTags) { - publisherTags(); - hasEnabledPublisherTags = true; - } + publisherTags(); } } diff --git a/src/components/SEO/publisherTags.js b/src/components/SEO/publisherTags.js index 3d38a0b2..83aabeb3 100644 --- a/src/components/SEO/publisherTags.js +++ b/src/components/SEO/publisherTags.js @@ -1,7 +1,13 @@ export default () => { + const { protocol } = document.location; + const gptScript = document.querySelector( + `script[src="${protocol}//www.googletagservices.com/tag/js/gpt.js"]` + ); + // Only do this once. + if (gptScript) return; const googletag = window.googletag || {}; googletag.cmd = googletag.cmd || []; - (function() { + (function () { const gads = document.createElement('script'); gads.async = true; gads.type = 'text/javascript'; @@ -12,7 +18,7 @@ export default () => { const node = document.getElementsByTagName('script')[0]; node.parentNode.insertBefore(gads, node); })(); - googletag.cmd.push(function() { + googletag.cmd.push(function () { googletag .defineSlot( '/4735792/reuters_investigates',