Skip to content

Commit

Permalink
Add GA4 utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Chiang committed Apr 29, 2022
1 parent 7540097 commit f632678
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/__utils__/gtags.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
declare global {
interface Window {
gtag: (
option: string,
gaTrackingId: string,
options: Record<string, unknown>
) => void;
}
}

/**
* @example
* https://developers.google.com/analytics/devguides/collection/gtagjs/pages
*/
export const pageview = (url: string, trackingId: string) => {
if (!window.gtag) {
console.warn(
"window.gtag is not defined. This could mean your google anylatics script has not loaded on the page yet."
);
return;
}
window.gtag("config", trackingId, {
page_path: url,
});
};

/**
* @example
* https://developers.google.com/analytics/devguides/collection/gtagjs/events
*/
export const event = ({
action,
category,
label,
value,
}: Record<string, string>) => {
if (!window.gtag) {
console.warn(
"window.gtag is not defined. This could mean your google anylatics script has not loaded on the page yet."
);
return;
}
window.gtag("event", action, {
event_category: category,
event_label: label,
value: value,
});
};
1 change: 1 addition & 0 deletions app/__utils__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as gtag from "./gtags.client";

0 comments on commit f632678

Please sign in to comment.