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

Commit 8fddca1

Browse files
feat(config): id accepts a function, promise or a function that returns promise
1 parent 0fb4aba commit 8fddca1

File tree

7 files changed

+61
-21
lines changed

7 files changed

+61
-21
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ npm install vue-analytics
4343
* [Set](/docs/set.md)
4444
* [Social interactions](/docs/social-interactions.md)
4545
* [User explorer report](/docs/user-explorer.md)
46-
* [Track multiple accounts](/docs/track-multiple-accounts.md)
4746
* [On Analaytics script ready](/docs/when-google-analytics-is-loaded.md)
4847
* [Custom methods](/docs/custom-methods.md)
4948
* [Ecommerce](/docs/ecommerce.md)

SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* [Set](/docs/set.md)
1414
* [Social interactions](/docs/social-interactions.md)
1515
* [User explorer report](/docs/user-explorer.md)
16-
* [Track multiple accounts](/docs/track-multiple-accounts.md)
1716
* [On Analaytics script ready](/docs/when-google-analytics-is-loaded.md)
1817
* [Custom methods](/docs/custom-methods.md)
1918
* [Ecommerce](/docs/ecommerce.md)

docs/installation.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,47 @@ Vue.use(VueAnalytics, {
1414
id: 'UA-XXX-X'
1515
})
1616
```
17+
18+
## Track multiple accounts
19+
20+
Pass an array of strings for a multiple tracking system. Every hit will be fired twice: each time with a different tracker name
21+
22+
```js
23+
import Vue from 'vue'
24+
import VueAnalytics from 'vue-analytics'
25+
26+
Vue.use(VueAnalytics, {
27+
id: ['UA-XXX-A', 'UA-XXX-B']
28+
})
29+
```
30+
31+
## Use functions or/and Promises
32+
33+
It is also possible to pass a function, a Promise or a function that returns a Promise: as soon as it returns always a string or an array of strings
34+
35+
```js
36+
import Vue from 'vue'
37+
import VueAnalytics from 'vue-analytics'
38+
import axios from 'axios'
39+
40+
// a function
41+
Vue.use(VueAnalytics, {
42+
id () {
43+
return 'UA-XXX-A'
44+
}
45+
})
46+
47+
// a Promise
48+
Vue.use(VueAnalytics, {
49+
id: axios.get('/api/foo').then(response => {
50+
return response.data
51+
})
52+
})
53+
54+
// a function that returns a Promise
55+
Vue.use(VueAnalytics, {
56+
id: () => axios.get('/api/foo').then(response => {
57+
return response.data
58+
})
59+
})
60+
```

docs/track-multiple-accounts.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/bootstrap.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ export default function bootstrap () {
3838
return onAnalyticsReady()
3939
})
4040
.then(() => {
41+
let newId = id
42+
43+
if (typeof newId === 'function') {
44+
newId = newId()
45+
}
46+
47+
if (typeof newId.then === 'function') {
48+
return newId.then(response => {
49+
config.id = response
50+
})
51+
}
52+
53+
return newId
54+
})
55+
.then(response => {
4156
// Create analytics trackers first
4257
createTrackers()
4358
// Add all collectors

src/directives/ga.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
let fn = typeof value === 'string'
77
? config.commands[value]
88
: value
9-
9+
1010
if (!fn) {
1111
throw new Error('[vue-analytics] The value passed to v-ga is not defined in the commands list.')
1212
}

src/lib/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getMethod } from '../helpers'
33

44
export default function query (method, ...args) {
55
getId().forEach(function (id) {
6-
if (typeof window.ga === 'undefined') {
6+
if (typeof window.ga === 'undefined' || typeof id !== 'string') {
77
config.untracked.push({
88
method: getMethod(method, id),
99
arguments: [...args]

0 commit comments

Comments
 (0)