From d9245f3e6e7b6c2127e2aa1feba6132dec81799d Mon Sep 17 00:00:00 2001 From: Pini Houri Date: Mon, 13 Aug 2018 10:40:24 +0300 Subject: [PATCH] feat(config): Allow setting different fields per ID --- __tests__/create-trackers.spec.js | 29 +++++++++++++++++++++++++++++ docs/user-explorer.md | 20 ++++++++++++++++++++ src/config.js | 1 + src/create-trackers.js | 3 ++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/__tests__/create-trackers.spec.js b/__tests__/create-trackers.spec.js index 13cb157..43a34ac 100644 --- a/__tests__/create-trackers.spec.js +++ b/__tests__/create-trackers.spec.js @@ -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', diff --git a/docs/user-explorer.md b/docs/user-explorer.md index dbed2bf..951652f 100644 --- a/docs/user-explorer.md +++ b/docs/user-explorer.md @@ -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** diff --git a/src/config.js b/src/config.js index ea2e364..ba19f6c 100644 --- a/src/config.js +++ b/src/config.js @@ -5,6 +5,7 @@ const defaultConfig = { id: null, router: null, fields: {}, + customIdFields: {}, ignoreRoutes: [], linkers: [], commands: {}, diff --git a/src/create-trackers.js b/src/create-trackers.js index e258c6a..94dffac 100644 --- a/src/create-trackers.js +++ b/src/create-trackers.js @@ -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) })