Skip to content

Commit

Permalink
Fix Google Analytics for real this time
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Dec 6, 2023
1 parent 7ed4cb5 commit 482d068
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions src/app/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ declare global {
}
}

export function ga(...args: unknown[]) {
(window.dataLayer ??= []).push(args);
window.dataLayer ||= [];

export function ga(..._args: any[]) {
// Google Analytics actually requires that we push arguments here, not _args!
// eslint-disable-next-line prefer-rest-params
window.dataLayer.push(arguments);
}

export function gaPageView(path: string, title?: string) {
ga('event', 'page_view', {
page_title: title,
page_location: window.location.origin + path,
page_path: path,
dim_version: $DIM_VERSION,
dim_flavor: $DIM_FLAVOR,
});
}

Expand All @@ -25,36 +27,20 @@ export function gaEvent(type: string, params: Record<string, string>) {
}

export function initGoogleAnalytics() {
ga('js', new Date());
const token = getToken();
ga('config', $ANALYTICS_PROPERTY, {
allow_ad_personalization_signals: false,
allow_google_signals: false,
send_page_view: false,
user_id: token?.bungieMembershipId,
dim_version: $DIM_VERSION,
dim_flavor: $DIM_FLAVOR,
});

const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
// ensure PageViews is always tracked (on script load)
script.onload = () => {
window.dataLayer ??= [];
// https:// constantsolutions.dk/2020/06/delay-loading-of-google-analytics-google-tag-manager-script-for-better-pagespeed-score-and-initial-load/
window.dataLayer.push({
event: 'gtm.js',
'gtm.start': new Date().getTime(),
'gtm.uniqueEventId': 0,
dim_version: $DIM_VERSION,
dim_flavor: $DIM_FLAVOR,
});
ga('js', new Date());
const token = getToken();
ga('config', $ANALYTICS_PROPERTY, {
store_gac: false,
allow_ad_personalization_signals: false,
allow_google_signals: false,
send_page_view: false,
dim_version: $DIM_VERSION,
dim_flavor: $DIM_FLAVOR,
user_id: token?.bungieMembershipId,
});
ga('set', {
dim_version: $DIM_VERSION,
dim_flavor: $DIM_FLAVOR,
});
};
script.src = `https://www.googletagmanager.com/gtag/js?id=${$ANALYTICS_PROPERTY}`;
document.head.appendChild(script);
}

0 comments on commit 482d068

Please sign in to comment.