JSFiddle - React, Tailwind, and code Playground
by David Simpson
JavaScript
var customerId = 'CUSTOMER_ID',
userId = 'USER_ID',
communityId = 'COMMUNITY_ID';
// ********************************************
// START: SETUP DEFAULT GOOGLE ANALYTICS TRACKING
(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');
//ga('create', 'UA-83020829-1', 'auto'); // default
ga('create', 'UA-83020829-1', {'userId': userId}); // For cross-device tracking of users
ga('set', 'dimension1', customerId); // Session based.
ga('set', 'dimension2', userId); // Session based. PII, but complies with section 7 of Google Analytics ToS
ga('set', 'dimension3', communityId); // Hit based.
ga('send', 'pageview');
// END: SETUP DEFAULT GOOGLE ANALYTICS TRACKING
// ********************************************
// $(function () { // document.ready shorthand
// ********************************************
// START: SETUP GOOGLE ANALYTICS AJAX EVENT HANDLING
// Inject Google Analytics event tracking into the global AJAX complete call:
$(document).ajaxComplete(function (event, xhr, settings) {
// https://developers.google.com/analytics/devguides/collection/analyticsjs/events
ga('send', {
hitType: 'event',
eventCategory: 'ajax.complete',
eventAction: '' + xhr.status,
eventLabel: settings.type + ': ' + settings.url
});
});
// END: SETUP GOOGLE ANALYTICS AJAX EVENT HANDLING
// ********************************************
// ********************************************
// EXAMPLE AJAX CALLS
// Example fail with a 404
$.ajax({
url: "/xxx_404_xxx",
type: "GET"
});
// Example success with 200
$.ajax({
url: "/echo/xml/",
type: "POST",
data: {
//Set an empty response to...