Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created July 1, 2021 12:04
Show Gist options
  • Select an option

  • Save nfreear/0146258d72e9e1330b19a1ff2c143ff6 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/0146258d72e9e1330b19a1ff2c143ff6 to your computer and use it in GitHub Desktop.
/**
* Analytics without cookies ~ Yay!
*
* @copyright Nick Freear, 28-June-2021.
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#using_localstorage_to_store_the_client_id;
*/
const ANALYTICS_ID: string = 'UA-XXXXXX-YY';
const GA_LOCAL_STORAGE_KEY: string = 'ga:clientId';
const GA_DEBUG = false;
export class Analytics {
protected analyticsId: string;
protected debug: boolean;
//.
public constructor(analyticsId = ANALYTICS_ID, debug = GA_DEBUG) {
this.analyticsId = analyticsId;
this.debug = debug;
this.injectScript();
this.sendPageView();
}
protected injectScript(): void {
/* eslint-disable */
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
/* @ts-ignore */
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script',`https://www.google-analytics.com/analytics${this.debug ? '_debug' : ''}.js`,'ga');
/* eslint-enable */
}
protected sendPageView(): void {
const ga = (window as any).ga;
if (window.localStorage) {
ga('create', this.analyticsId, {
'storage': 'none',
'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga((tracker: any): void => {
localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});
} else {
ga('create', this.analyticsId, 'auto');
}
ga('send', 'pageview');
console.debug('Analytics ~ sendPageView:', this.analyticsId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment