Skip to content

Commit

Permalink
Use GA4 event tracking
Browse files Browse the repository at this point in the history
- replaces measurement protocol approach
  • Loading branch information
ajparsons committed Jun 7, 2023
1 parent 0ce0799 commit bc9eff5
Showing 1 changed file with 7 additions and 38 deletions.
45 changes: 7 additions & 38 deletions www/docs/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,15 @@ var trackEvent = function(eventName, params) {
dfd.resolve();
};

// Returns either an array, or undefined.
var measurementProtocolDetails = window.dataLayer.filter(function(row){
return row[0] === 'measurement_protocol';
})[0];

if (measurementProtocolDetails) {
var measurementParams = {
measurement_id: measurementProtocolDetails[1],
api_secret: measurementProtocolDetails[2]
}
if (measurementProtocolDetails[3]) {
params['debug_mode'] = '1';
}

// Set a random client_id (2 32-bit integers seperated by a dot).
// Note this random approach means the GA debugView won't work.
// (To get that to work, you need to turn back on the cookies,
// then use the same client_id as in in the _ga cookie.)
var client_id = Math.floor(Math.random() * 1000000000) + '.' + Math.floor(Math.random() * 1000000000);

// print response to console
$.ajax({
url: 'https://www.google-analytics.com/mp/collect?' + $.param(measurementParams),
method: 'POST',
data: JSON.stringify({
client_id: client_id,
events: [{
name: eventName,
params: params
}]
})
}).always(callback);
// Tell Gtag to resolve our promise when it's done.
var params = $.extend(params, {
event_callback: callback
});

// Wait a maximum of 2 seconds for ajax to resolve.
setTimeout(callback, 2000);
gtag('event', eventName, params);

} else {
// Measurement Protocol not available. Resolve promise immediately.
callback();
}
// Wait a maximum of 2 seconds for Gtag to resolve promise.
setTimeout(callback, 2000);

return dfd.promise();
};
Expand Down

0 comments on commit bc9eff5

Please sign in to comment.