types and cleanup

This commit is contained in:
Jon McClure 2023-05-28 22:40:23 +01:00
parent d3d917b74f
commit fbea3a560f
3 changed files with 35 additions and 23 deletions

View file

@ -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,

View file

@ -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}`); }
};

23
src/globals.d.ts vendored Normal file
View file

@ -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<string, any>,
/** Chartbeat config */
_sf_async_config: ChartbeatConfig,
/** Chartbeat method */
pSUPERFLY: (config: { path: string, title: string }) => void,
}
}
export {};