Skip to content

Commit

Permalink
Pzl.Ext.GoogleAnalytics - Tracker id starts with "GTM-"- or "UA-"
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGranheim committed Jan 9, 2019
1 parent 03ade56 commit d3f329b
Showing 1 changed file with 28 additions and 18 deletions.
Expand Up @@ -58,25 +58,35 @@ export default class GoogleAnalyticsApplicationCustomizer
private track(): void {
let trackerID: string = this.properties.trackerID;
if (trackerID) {
let iframe: any = document.createElement("iframe");
iframe.id = "GoogleAnalyticsApplicationCustomizerIframe";
iframe.height = "0";
iframe.width = "0";
iframe.style = "display:none;visibility:hidden";
iframe.src = `https://www.googletagmanager.com/ns.html?id=${trackerID}`;

let element = document.getElementById("GoogleAnalyticsApplicationCustomizerIframe");
if (typeof (element) !== 'undefined' && element !== null) {
document.body.removeChild(element);
if(trackerID.startsWith("GTM-")) {
let iframe: any = document.createElement("iframe");
iframe.id = "GoogleAnalyticsApplicationCustomizerIframe";
iframe.height = "0";
iframe.width = "0";
iframe.style = "display:none;visibility:hidden";
iframe.src = `https://www.googletagmanager.com/ns.html?id=${trackerID}`;

let element = document.getElementById("GoogleAnalyticsApplicationCustomizerIframe");
if (typeof (element) !== 'undefined' && element !== null) {
document.body.removeChild(element);
}
document.body.appendChild(iframe);
eval(`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${trackerID}');
`);
Log.info(LOG_SOURCE, `Tracking with ID ${this.properties.trackerID}`);
} else if (trackerID.startsWith("UA-")) {
eval(`(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', '${trackerID}', 'auto');
ga('send', 'pageview');`);
Log.info(LOG_SOURCE, `Tracking with ID ${this.properties.trackerID}`);
}
document.body.appendChild(iframe);
eval(`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${trackerID}');
`);
Log.info(LOG_SOURCE, `Tracking with ID ${this.properties.trackerID}`);
} else {
Log.info(LOG_SOURCE, "Tracking ID not provided");
}
Expand Down

0 comments on commit d3f329b

Please sign in to comment.