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

Adds ssGTM proxy config for gtag #101

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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.yarn.lock
.DS_Store
/services
.idea
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ Data layer to be set before GTM is loaded. Should be an object or a function tha
}
```

#### `transportUrl`

Use this when using a server side Google Tag manager proxy. Will replace https://www.googletagmanager.com with YOUR_DOMAIN.

See more at [developers.google.com](https://developers.google.com/tag-platform/tag-manager/server-side/send-data).

#### `firstPartyCollection`

Used when using a server side Google Tag manager proxy. Defaults to `false` and must be set to `true` when `transportUrl`
is set.

#### Tracking routes

Out of the box this plugin will simply load Google Tag Manager on the initial page/app load. It’s up to you to fire tags based on changes in your app.
Expand Down
4 changes: 3 additions & 1 deletion src/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const defaultOptions = {
googleTagManager: {
cookieName: `gatsby-gdpr-google-tagmanager`,
dataLayerName: `dataLayer`,
routeChangeEvent: `gatsbyRouteChange`
routeChangeEvent: `gatsbyRouteChange`,
transportUrl: ``,
firstPartyCollection: false
},
facebookPixel: {
cookieName: `gatsby-gdpr-facebook-pixel`
Expand Down
12 changes: 9 additions & 3 deletions src/services/google-tag-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { validGTMTrackingId, getCookie } = require(`../helper`)

const GOOGLE_TAG_MANAGER_TRANSPORT_URL = 'https://www.googletagmanager.com';

exports.addGoogleTagManager = (
{ trackingId, dataLayerName },
{ trackingId, dataLayerName, transportUrl, firstPartyCollection },
environmentParamStr
) => {
return new Promise((resolve, reject) => {
Expand All @@ -20,7 +22,7 @@ exports.addGoogleTagManager = (
dl = l != "dataLayer" ? "&l=" + l : ""
j.async = true
j.src =
"https://www.googletagmanager.com/gtm.js?id=" +
`${firstPartyCollection ? transportUrl : GOOGLE_TAG_MANAGER_TRANSPORT_URL}/gtm.js?id=` +
i +
dl +
environmentParamStr
Expand All @@ -30,7 +32,9 @@ exports.addGoogleTagManager = (

const iframe = document.createElement(`iframe`)
iframe.key = `gatsby-plugin-gdpr-cookies-google-tagmanager-iframe`
iframe.src = `https://www.googletagmanager.com/ns.html?id=${trackingId}${environmentParamStr}`
iframe.src = `${
firstPartyCollection ? transportUrl : GOOGLE_TAG_MANAGER_TRANSPORT_URL
}/ns.html?id=${trackingId}${environmentParamStr}`
iframe.height = 0
iframe.width = 0
iframe.style = `display: none; visibility: hidden`
Expand Down Expand Up @@ -65,6 +69,8 @@ exports.initializeGoogleTagManager = (options) => {
window.gtag(`config`, options.trackingId, {
anonymize_ip: gaAnonymize,
allow_google_signals: gaAllowAdFeatures,
transport_url: options.firstPartyCollection ? options.transportUrl : GOOGLE_TAG_MANAGER_TRANSPORT_URL,
first_party_collection: options.firstPartyCollection
})
}
}
Expand Down
Loading