diff --git a/src/components/Analytics/providers/chartbeat.ts b/src/components/Analytics/providers/chartbeat.ts index 6fd791c6..b20e2c53 100644 --- a/src/components/Analytics/providers/chartbeat.ts +++ b/src/components/Analytics/providers/chartbeat.ts @@ -1,26 +1,21 @@ // Reuters Chartbeat UID const UID = 52639; -/* eslint-disable */ +const URL = '//static.chartbeat.com/js/chartbeat.js'; + const attachScript = () => { // If script is already attached, skip - if ( - document.querySelector( - 'script[src="//static.chartbeat.com/js/chartbeat.js"]' - ) - ) { return; } + if (document.querySelector(`script[src="${URL}"]`)) return; // ... else attach it. const e = document.createElement('script'); const n = document.getElementsByTagName('script')[0]; e.type = 'text/javascript'; e.async = true; - e.src = '//static.chartbeat.com/js/chartbeat.js'; + e.src = URL; n.parentNode.insertBefore(e, n); }; -/* eslint-enable */ export default (authors: { name: string }[]) => { - // @ts-ignore const config = window._sf_async_config = (window._sf_async_config || {}); config.uid = UID; config.domain = 'reuters.com'; @@ -36,9 +31,7 @@ export default (authors: { name: string }[]) => { }; export const registerPageview = () => { - // @ts-ignore if (!window.pSUPERFLY) return; - // @ts-ignore window.pSUPERFLY({ path: window.location.pathname, title: document.title, diff --git a/src/components/Analytics/providers/ga.ts b/src/components/Analytics/providers/ga.ts index c4674f89..7a836725 100644 --- a/src/components/Analytics/providers/ga.ts +++ b/src/components/Analytics/providers/ga.ts @@ -1,38 +1,34 @@ // Reuters Google Tag ID const GOOGLE_TAG_ID = 'G-W3Q2X6NTNM'; -/* eslint-disable */ +const URL = `https://www.googletagmanager.com/gtag/js?id=${GOOGLE_TAG_ID}`; + const attachScript = () => { // If script is already attached, skip - if ( - document.querySelector( - `script[src="https://www.googletagmanager.com/gtag/js?id=${GOOGLE_TAG_ID}"]` - ) - ) - return; + if (document.querySelector(`script[src="${URL}"]`)) return; // ... else attach it. const e = document.createElement('script'); const n = document.getElementsByTagName('script')[0]; e.type = 'text/javascript'; e.async = true; - e.src = `https://www.googletagmanager.com/gtag/js?id=${GOOGLE_TAG_ID}`; + e.src = URL; n.parentNode.insertBefore(e, n); }; -/* eslint-enable */ export default () => { try { - // @ts-ignore window.dataLayer = window.dataLayer || []; if (!window.gtag) { attachScript(); /** @type {Gtag.Gtag} */ window.gtag = (...args) => { - // @ts-ignore window.dataLayer.push(...args); }; window.gtag('js', new Date()); - window.gtag('config', GOOGLE_TAG_ID); + // config event registers a pageview by default + window.gtag('config', GOOGLE_TAG_ID, { + page_location: window.location.origin + window.location.pathname, + }); } } catch (e) { console.warn(`Error initialising Google Analytics: ${e}`); } }; diff --git a/src/globals.d.ts b/src/globals.d.ts new file mode 100644 index 00000000..dd4ce9e7 --- /dev/null +++ b/src/globals.d.ts @@ -0,0 +1,23 @@ +interface ChartbeatConfig { + uid?: number; + domain?: string; + flickerControl?: boolean; + useCanonical?: boolean; + useCanonicalDomain?: boolean; + sections?: string; + authors?: string; +} + +declare global { + // eslint-disable-next-line no-unused-vars + interface Window { + /** Google analytics dataLayer */ + dataLayer: Record, + /** Chartbeat config */ + _sf_async_config: ChartbeatConfig, + /** Chartbeat method */ + pSUPERFLY: (config: { path: string, title: string }) => void, + } +} + +export {};