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

feat(Adding GTM Server-side Custom URL) #151

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"description": "Enter the ID (GTM-XXXX) from your Google Tag Manager",
"type": "string"
},
"gtmUrl": {
"title": "Google Tag Manager Server URL",
"description": "Enter the URL from your Google Tag Manager Server",
"type": "string"
},
"allowCustomHtmlTags": {
"title": "Allow Custom HTML tags",
"description": "Beware that using Custom HTML tags can drastically impact the store's performance",
Expand Down
16 changes: 13 additions & 3 deletions pixel/head.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<script>
(function() {
var gtmId = "{{settings.gtmId}}";
let gtmUrl = "{{settings.gtmUrl}}";
var sendGA4EventsValue = "{{settings.sendGA4Events}}"
var sendGA4Events = sendGA4EventsValue == 'true'

window.__gtm__ = {
sendGA4Events
}

const gtmUrlDefault = 'https://www.googletagmanager.com/gtm.js?id='

if (gtmUrl) {
gtmId = gtmId.replace('GTM-', '')
gtmUrl = decodeURIComponent(gtmUrl)
} else {
gtmUrl = gtmUrlDefault
}

if (!gtmId) {
console.error('Warning: No Google Tag Manager ID is defined. Please configure it in the apps admin.');
} else {
Expand All @@ -16,9 +26,9 @@
// GTM script snippet. Taken from: https://developers.google.com/tag-manager/quickstart
(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);
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=gtmUrl+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer',gtmId)
}
})()
</script>