Skip to content

Added the ability to override the default gtm url #129

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

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Look for gtm_auth and gtm_preview
|events| `Object`| No | Additional events such as 'gtm.start': new Date().getTime(),event:'gtm.js'.|
|auth| `String` | No | used to set environments. |
|preview| `String` | No | used to set environments, something like `env-00`. |
|url| `String` | No | used to override the default gtm url. |


### Note:
Expand Down
9 changes: 6 additions & 3 deletions src/Snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import warn from './utils/warn'
// https://developers.google.com/tag-manager/quickstart

const Snippets = {
tags: function ({ id, events, dataLayer, dataLayerName, preview, auth }) {
tags: function ({ id, events, dataLayer, dataLayerName, preview, auth, url }) {
const gtm_auth = `&gtm_auth=${auth}`
const gtm_preview = `&gtm_preview=${preview}`
const pattern = /^(https:\/\/)/;

if (!id) warn('GTM Id is required')

if(!pattern.test(url)) warn('URL needs to be a valid url')

const iframe = `
<iframe src="https://www.googletagmanager.com/ns.html?id=${id}${gtm_auth}${gtm_preview}&gtm_cookies_win=x"
<iframe src="${url}/ns.html?id=${id}${gtm_auth}${gtm_preview}&gtm_cookies_win=x"
height="0" width="0" style="display:none;visibility:hidden" id="tag-manager"></iframe>`

const script = `
(function(w,d,s,l,i){w[l]=w[l]||[];
w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js', ${JSON.stringify(events).slice(1, -1)}});
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+'${gtm_auth}${gtm_preview}&gtm_cookies_win=x';
j.async=true;j.src='${url}/gtm.js?id='+i+dl+'${gtm_auth}${gtm_preview}&gtm_cookies_win=x';
f.parentNode.insertBefore(j,f);
})(window,document,'script','${dataLayerName}','${id}');`

Expand Down
5 changes: 3 additions & 2 deletions src/TagManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ const TagManager = {
dataScript
}
},
initialize: function ({ gtmId, events = {}, dataLayer, dataLayerName = 'dataLayer', auth = '', preview = '' }) {
initialize: function ({ gtmId, events = {}, dataLayer, dataLayerName = 'dataLayer', auth = '', preview = '', url = "https://www.googletagmanager.com"}) {
const gtm = this.gtm({
id: gtmId,
events: events,
dataLayer: dataLayer || undefined,
dataLayerName: dataLayerName,
auth,
preview
preview,
url
})
if (dataLayer) document.head.appendChild(gtm.dataScript)
document.head.insertBefore(gtm.script(), document.head.childNodes[0])
Expand Down