Skip to content

Commit

Permalink
Start using GA4 (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iinh authored Feb 5, 2024
1 parent 4504a9b commit e3d45e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions src/telemetry/ga.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const googleAnalytics = (gaID) => {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(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.js','ga');
const script = document.createElement('script');
script.src = `https://www.googletagmanager.com/gtag/js?id=${gaID}`;
script.async = true;
document.head.appendChild(script);

ga('create', gaID, 'auto');
};
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', gaID);
};
10 changes: 5 additions & 5 deletions src/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export function initializeTelemetry() {
* @param {string} path The current page path.
*/
export function submitPageViewTelemetry(path) {
// Send telemetry to Google Analytics.
if (window.ga) {
// `ga` will not be set if not using google analytics
ga("set", "page", path);
ga("send", "pageview");
// Send page view event to GA4.
if (window.gtag) {
window.gtag("event", "page_view", {
page_path: path,
});
}

// Use the standard events.
Expand Down

0 comments on commit e3d45e0

Please sign in to comment.