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

Commit

Permalink
feat(config): Allow setting different fields per ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Pini Houri committed Aug 13, 2018
1 parent c81a5eb commit d9245f3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
29 changes: 29 additions & 0 deletions __tests__/create-trackers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ it ('should initialize multiple trackers', () => {
})
})

it('should intialize each id with its own configuration', () => {
const customIdFields = {
'UA-12345-1': {
clientId: '1'
},
'UA-54321-1': {
clientId: '2'
},
}

mockUpdate({
id: ['UA-12345-1', 'UA-54321-1'],
fields: {
'global': true
},
customIdFields,
})

createTrackers()

mockGetId().forEach(id => {
expect(window.ga).toBeCalledWith('create', id, 'auto', {
global: true,
...customIdFields[id],
name: getTracker(id),
})
})
})

it ('should add linkers if list is not empty', function () {
mockUpdate({
id: 'UA-1234-1',
Expand Down
20 changes: 20 additions & 0 deletions docs/user-explorer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ Vue.use(VueAnalytics, {
})
```

It's also possible to add fields per id, useful for read only fields.

```js
Vue.use(VueAnalytics, {
id: ['UA-12345-1', 'UA-54321-2'],
//fields for both IDS
fields: {
userId: '1',
},
customIdFields: {
'UA-12345-1': {
clientId: '2'
},
'UA-54321-2': {
clientId: '3'
}
}
})
```

**it is also possible to set the **`userId`** in runtime using the **[**set**](/docs/set.md)** method**
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const defaultConfig = {
id: null,
router: null,
fields: {},
customIdFields: {},
ignoreRoutes: [],
linkers: [],
commands: {},
Expand Down
3 changes: 2 additions & 1 deletion src/create-trackers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default function createTrackers () {

ids.forEach(function (domain) {
const name = getTracker(domain)
const options = ids.length > 1 ? { ...config.fields, name } : config.fields
const customIdConfig = config.customIdFields[domain] || {}
const options = ids.length > 1 ? { ...config.fields, ...customIdConfig, name } : config.fields

window.ga('create', (domain.id || domain), 'auto', options)
})
Expand Down

0 comments on commit d9245f3

Please sign in to comment.