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

Commit

Permalink
feat(config): id accepts a function, promise or a function that retur…
Browse files Browse the repository at this point in the history
…ns promise
  • Loading branch information
MatteoGabriele committed Dec 16, 2017
1 parent 0fb4aba commit 8fddca1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 21 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ npm install vue-analytics
* [Set](/docs/set.md)
* [Social interactions](/docs/social-interactions.md)
* [User explorer report](/docs/user-explorer.md)
* [Track multiple accounts](/docs/track-multiple-accounts.md)
* [On Analaytics script ready](/docs/when-google-analytics-is-loaded.md)
* [Custom methods](/docs/custom-methods.md)
* [Ecommerce](/docs/ecommerce.md)
Expand Down
1 change: 0 additions & 1 deletion SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* [Set](/docs/set.md)
* [Social interactions](/docs/social-interactions.md)
* [User explorer report](/docs/user-explorer.md)
* [Track multiple accounts](/docs/track-multiple-accounts.md)
* [On Analaytics script ready](/docs/when-google-analytics-is-loaded.md)
* [Custom methods](/docs/custom-methods.md)
* [Ecommerce](/docs/ecommerce.md)
Expand Down
44 changes: 44 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,47 @@ Vue.use(VueAnalytics, {
id: 'UA-XXX-X'
})
```

## Track multiple accounts

Pass an array of strings for a multiple tracking system. Every hit will be fired twice: each time with a different tracker name

```js
import Vue from 'vue'
import VueAnalytics from 'vue-analytics'

Vue.use(VueAnalytics, {
id: ['UA-XXX-A', 'UA-XXX-B']
})
```

## Use functions or/and Promises

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

```js
import Vue from 'vue'
import VueAnalytics from 'vue-analytics'
import axios from 'axios'

// a function
Vue.use(VueAnalytics, {
id () {
return 'UA-XXX-A'
}
})

// a Promise
Vue.use(VueAnalytics, {
id: axios.get('/api/foo').then(response => {
return response.data
})
})

// a function that returns a Promise
Vue.use(VueAnalytics, {
id: () => axios.get('/api/foo').then(response => {
return response.data
})
})
```
17 changes: 0 additions & 17 deletions docs/track-multiple-accounts.md

This file was deleted.

15 changes: 15 additions & 0 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ export default function bootstrap () {
return onAnalyticsReady()
})
.then(() => {
let newId = id

if (typeof newId === 'function') {
newId = newId()
}

if (typeof newId.then === 'function') {
return newId.then(response => {
config.id = response
})
}

return newId
})
.then(response => {
// Create analytics trackers first
createTrackers()
// Add all collectors
Expand Down
2 changes: 1 addition & 1 deletion src/directives/ga.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
let fn = typeof value === 'string'
? config.commands[value]
: value

if (!fn) {
throw new Error('[vue-analytics] The value passed to v-ga is not defined in the commands list.')
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getMethod } from '../helpers'

export default function query (method, ...args) {
getId().forEach(function (id) {
if (typeof window.ga === 'undefined') {
if (typeof window.ga === 'undefined' || typeof id !== 'string') {
config.untracked.push({
method: getMethod(method, id),
arguments: [...args]
Expand Down

0 comments on commit 8fddca1

Please sign in to comment.