mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 19:03:30 -05:00
Wrap analytics code in GOVUK interface
Wraps our analytics code in a stripped down version of GOVUK.Analytics to allow us to plug in the GOVUK code for consent.
This commit is contained in:
39
app/assets/javascripts/analytics/analytics.js
Normal file
39
app/assets/javascripts/analytics/analytics.js
Normal file
@@ -0,0 +1,39 @@
|
||||
(function (window) {
|
||||
"use strict";
|
||||
|
||||
window.GOVUK = window.GOVUK || {};
|
||||
|
||||
// Stripped-down wrapper for Google Analytics, based on:
|
||||
// https://github.com/alphagov/static/blob/master/app/assets/javascripts/analytics_toolkit/analytics.js
|
||||
const Analytics = function (config) {
|
||||
window.ga('create', config.trackingId, config.cookieDomain);
|
||||
|
||||
window.ga('set', 'anonymizeIp', config.anonymizeIp);
|
||||
window.ga('set', 'displayFeaturesTask', config.displayFeaturesTask);
|
||||
window.ga('set', 'transport', config.transport);
|
||||
|
||||
};
|
||||
|
||||
Analytics.load = function () {
|
||||
/* jshint ignore:start */
|
||||
(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','//www.google-analytics.com/analytics.js','ga');
|
||||
/* jshint ignore:end */
|
||||
|
||||
};
|
||||
|
||||
Analytics.prototype.trackPageview = function (path, title, options) {
|
||||
|
||||
// strip UUIDs
|
||||
const page = (window.location.pathname + window.location.search).replace(
|
||||
/[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}/g, '…'
|
||||
);
|
||||
window.ga('send', 'pageview', page);
|
||||
|
||||
};
|
||||
|
||||
window.GOVUK.Analytics = Analytics;
|
||||
|
||||
})(window);
|
||||
39
app/assets/javascripts/analytics/init.js
Normal file
39
app/assets/javascripts/analytics/init.js
Normal file
@@ -0,0 +1,39 @@
|
||||
(function (window) {
|
||||
"use strict";
|
||||
|
||||
window.GOVUK = window.GOVUK || {};
|
||||
|
||||
const trackingId = 'UA-26179049-1';
|
||||
|
||||
// Disable analytics by default
|
||||
window[`ga-disable-${trackingId}`] = true;
|
||||
|
||||
const initAnalytics = function () {
|
||||
|
||||
// guard against being called more than once
|
||||
if (!('analytics' in window.GOVUK)) {
|
||||
|
||||
window[`ga-disable-${trackingId}`] = false;
|
||||
|
||||
// Load Google Analytics libraries
|
||||
window.GOVUK.Analytics.load();
|
||||
|
||||
// Configure profiles and make interface public
|
||||
// for custom dimensions, virtual pageviews and events
|
||||
window.GOVUK.analytics = new GOVUK.Analytics({
|
||||
trackingId: trackingId,
|
||||
cookieDomain: 'auto',
|
||||
anonymizeIp: true,
|
||||
displayFeaturesTask: null,
|
||||
transport: 'beacon'
|
||||
});
|
||||
|
||||
// Track initial pageview
|
||||
window.GOVUK.analytics.trackPageview();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
window.GOVUK.initAnalytics = initAnalytics;
|
||||
})(window);
|
||||
@@ -2,6 +2,8 @@ window.GOVUK.Frontend.initAll();
|
||||
|
||||
$(() => GOVUK.addCookieMessage());
|
||||
|
||||
window.GOVUK.initAnalytics();
|
||||
|
||||
$(() => $("time.timeago").timeago());
|
||||
|
||||
$(() => GOVUK.stickAtTopWhenScrolling.init());
|
||||
|
||||
Reference in New Issue
Block a user