Skip to content

Commit

Permalink
Merge pull request #62 from naseberry/peterhunt/gtag_environment_trac…
Browse files Browse the repository at this point in the history
…king

[WEBDEV-888] Dynamically add gtag tracking ID based on environment
  • Loading branch information
naseberry authored Nov 12, 2018
2 parents 2b856b0 + 5d3fa20 commit 7383f77
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 36 additions & 4 deletions resources/js/googleAnalytics.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
'use strict';

/*
* Google Analytics
*/

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-15845045-29', {'anonymize_ip': true});

(function(){

let GA_TRACKING_ID;
const GA_TRACKING_ID_WWW = 'UA-15845045-1';
const GA_TRACKING_ID_BETA = 'UA-15845045-29';
const GA_TRACKING_ID_DEVCI = 'UA-15845045-30';

const hostname = window.location.hostname;

switch(hostname) {
case 'www.parliament.uk':
GA_TRACKING_ID = GA_TRACKING_ID_WWW;
break;
case 'beta.parliament.uk':
GA_TRACKING_ID = GA_TRACKING_ID_BETA;
break;
case 'devci.parliament.uk':
GA_TRACKING_ID = GA_TRACKING_ID_DEVCI;
}


let gtagScript = document.createElement('script');
gtagScript.async = true;
gtagScript.setAttribute('src', `https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`);
document.head.appendChild(gtagScript);


window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', GA_TRACKING_ID, {'anonymize_ip': true});

})();
1 change: 0 additions & 1 deletion test/fixtures/html/components/head/script.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<script src="appInsights.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-15845045-29"></script>
<script src="googleAnalytics.js"></script>
1 change: 0 additions & 1 deletion test/fixtures/html/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<link href="https://static.parliament.uk/assets-public/favicon.ico" rel="shortcut icon" type="image/x-icon">
<link href="https://beta.parliament.uk/search/opensearch" rel="search" title="UK Parliament" type="application/opensearchdescription+xml">
<script src="appInsights.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-15845045-29"></script>
<script src="googleAnalytics.js"></script>
<link href="http://localhost:3030/api/v1/statutory_instruments.nt" rel="alternate" type="application/n-triples">
<link href="http://localhost:3030/api/v1/statutory_instruments.ttl" rel="alternate" type="text/turtle">
Expand Down
3 changes: 1 addition & 2 deletions view/components/head/script.dust
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<script src="{@assetPath src="appInsights.js"/}"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-15845045-29"></script>
<script src="{@assetPath src="googleAnalytics.js"/}"></script>
<script src="{@assetPath src="googleAnalytics.js"/}"></script>

0 comments on commit 7383f77

Please sign in to comment.