Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

feat: add GTM handler and options #43

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ npm install nuxt-google-optimize --save
// experimentsDir: '~/experiments',
// maxAge: 60 * 60 * 24 * 7 // 1 Week
// pushPlugin: true,
// eventHandler: 'ga', // 'ga' = Google Analytics integration || 'gtm' = @nuxtjs/gtm-module integration || 'dataLayer' = GTM integration
// dataLayer: 'dataLayer', // eventHandler option has to be 'dataLayer' when implementing custom GTM dataLayer
farzadso marked this conversation as resolved.
Show resolved Hide resolved
// excludeBots: true,
// botExpression: /(bot|spider|crawler)/i
}
Expand Down Expand Up @@ -222,6 +224,22 @@ import './styles.scss'
}
```

## Usage with GTM

- Set `options.eventHandler` to 'gtm' or 'dataLayer' depending which integration style you use for Tag Manager.
- Edit your "Page view (Google Analytics)" -tag in [Google Tag Manager](https://tagmanager.google.com/#/home)
- Add new "Fields to Set":
- Field Name: exp
- Value: {{googleOptimizeExp}}
- Add new Data Layer Variable called "googleOptimizeExp" as defined above.
- Variable type: Data Layer Variable
- Data Layer Variable Name: exp

[Source for this setup lossleader's answer in StackOverflow](https://stackoverflow.com/a/53253769/871677)

Now this module pushes experiment id and variable number to Google Analytics via Google Tag Manager.
experiment.experimentID + '.' + experiment.$variantIndexes.join('-')

## Development

- Clone this repository
Expand Down
2 changes: 2 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module.exports = async function module (moduleOptions) {
experimentsDir: '~/experiments',
maxAge: 60 * 60 * 24 * 7, // 1 Week
plugins: [],
eventHandler: 'ga',
dataLayer: 'dataLayer',
excludeBots: true,
botExpression: /(bot|spider|crawler)/i
},
Expand Down
28 changes: 23 additions & 5 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,32 @@ function setCookie(ctx, name, value, maxAge = <%= options.maxAge %>) {
}

// https://developers.google.com/optimize/devguides/experiments
function googleOptimize({ experiment }) {
if (process.server || !window.ga || !experiment || !experiment.experimentID) {
function googleOptimize(ctx) {
const { experiment } = ctx
if (process.server || !experiment || !experiment.experimentID) {
return
}

const exp = experiment.experimentID + '.' + experiment.$variantIndexes.join('-')

window.ga('set', 'exp', exp)
// Choose method to track experiment and variant
<% if (options.eventHandler === 'ga') { %>
// Use Google Analytics integration
// https://developers.google.com/optimize/devguides/experiments
const exp = experiment.experimentID + '.' + experiment.$variantIndexes.join('-')
if (!window.ga) return
window.ga('set', 'exp', exp)
<% } else if (options.eventHandler === 'gtm') { %>
// Use @nuxtjs/gtm-module
if (!ctx.$gtm) return
ctx.$gtm.push({
exp: exp
farzadso marked this conversation as resolved.
Show resolved Hide resolved
})
<% } else if (options.eventHandler === 'dataLayer') { %>
// Use other tag manager integration
if (!window[options.dataLayer]) return
window[options.dataLayer].push({
exp: exp
farzadso marked this conversation as resolved.
Show resolved Hide resolved
})
<% } %>
}

// should we skip bots?
Expand Down