Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analytics.js file with optional code #566

Open
wants to merge 4 commits into
base: development_3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/app/core/assets/js/analytics.custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var ga4JsLoaderRef=(function ()
{
var o = {};
o.ga4LoadJs = function(args) {
if(!(typeof args !== 'undefined' && args && args.GTAG_ID && args.GTAG_MGR_ID)) {
return o;
}
var gtagUrl = 'https://www.googletagmanager.com/gtag/js?id=' + args.GTAG_ID;
let scriptElement = document.createElement('script');
scriptElement.setAttribute('src', gtagUrl);
scriptElement.setAttribute('type', 'text/javascript');
scriptElement.setAttribute('async', true);
document.head.appendChild(scriptElement);
// success event
scriptElement.addEventListener('load', () => {
console.log('Analytics script loaded.');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); gtag('config', args.GTAG_ID);
(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', args.GTAG_MGR_ID);
});
// error event
scriptElement.addEventListener('error', (event) => {
console.log('Error on loading GA4 remove script', event);
});
}
return o;
});

// var gsrs_ga4LoadJs_args = {
// 'GTAG_ID': '',
// 'GTAG_MGR_ID': ''
// }

if(typeof gsrs_ga4LoadJs_args !== 'undefined') {
gsrs_ga4LoadJs = ga4JsLoaderRef();
gsrs_ga4LoadJs.ga4LoadJs(gsrs_ga4LoadJs_args);
}
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<html lang="en">

<head>
<script src="/assets/js/analytics.custom.js"></script>

<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
Expand Down
Loading