Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit d9245f3

Browse files
author
Pini Houri
committed
feat(config): Allow setting different fields per ID
1 parent c81a5eb commit d9245f3

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

__tests__/create-trackers.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ it ('should initialize multiple trackers', () => {
2828
})
2929
})
3030

31+
it('should intialize each id with its own configuration', () => {
32+
const customIdFields = {
33+
'UA-12345-1': {
34+
clientId: '1'
35+
},
36+
'UA-54321-1': {
37+
clientId: '2'
38+
},
39+
}
40+
41+
mockUpdate({
42+
id: ['UA-12345-1', 'UA-54321-1'],
43+
fields: {
44+
'global': true
45+
},
46+
customIdFields,
47+
})
48+
49+
createTrackers()
50+
51+
mockGetId().forEach(id => {
52+
expect(window.ga).toBeCalledWith('create', id, 'auto', {
53+
global: true,
54+
...customIdFields[id],
55+
name: getTracker(id),
56+
})
57+
})
58+
})
59+
3160
it ('should add linkers if list is not empty', function () {
3261
mockUpdate({
3362
id: 'UA-1234-1',

docs/user-explorer.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,24 @@ Vue.use(VueAnalytics, {
1111
})
1212
```
1313

14+
It's also possible to add fields per id, useful for read only fields.
15+
16+
```js
17+
Vue.use(VueAnalytics, {
18+
id: ['UA-12345-1', 'UA-54321-2'],
19+
//fields for both IDS
20+
fields: {
21+
userId: '1',
22+
},
23+
customIdFields: {
24+
'UA-12345-1': {
25+
clientId: '2'
26+
},
27+
'UA-54321-2': {
28+
clientId: '3'
29+
}
30+
}
31+
})
32+
```
33+
1434
**it is also possible to set the **`userId`** in runtime using the **[**set**](/docs/set.md)** method**

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const defaultConfig = {
55
id: null,
66
router: null,
77
fields: {},
8+
customIdFields: {},
89
ignoreRoutes: [],
910
linkers: [],
1011
commands: {},

src/create-trackers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export default function createTrackers () {
1414

1515
ids.forEach(function (domain) {
1616
const name = getTracker(domain)
17-
const options = ids.length > 1 ? { ...config.fields, name } : config.fields
17+
const customIdConfig = config.customIdFields[domain] || {}
18+
const options = ids.length > 1 ? { ...config.fields, ...customIdConfig, name } : config.fields
1819

1920
window.ga('create', (domain.id || domain), 'auto', options)
2021
})

0 commit comments

Comments
 (0)