From b1edc28b1f0d0c29f77f5e60acedbcb73f1821d2 Mon Sep 17 00:00:00 2001 From: Putt Date: Tue, 25 Dec 2018 20:36:12 +0700 Subject: [PATCH 1/3] add options identifyByUserId --- lib/index.js | 4 +++- test/index.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 3cc4484..83c7ab3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -54,7 +54,9 @@ var Visitor = module.exports.Visitor = function (tid, cid, options, context, per this._persistentParams = persistentParams || {}; this.tid = tid || this.options.tid; - this.cid = this._determineCid(cid, this.options.cid, (this.options.strictCidFormat !== false)); + if(!this.options.identifyByUserId || (this.options.identifyByUserId && !this.options.uid)) { + this.cid = this._determineCid(cid, this.options.cid, (this.options.strictCidFormat !== false)); + } if(this.options.uid) { this.uid = this.options.uid; } diff --git a/test/index.js b/test/index.js index 5bc78cb..e9e9313 100644 --- a/test/index.js +++ b/test/index.js @@ -114,6 +114,41 @@ describe("ua", function () { visitor.cid.should.equal(options.cid) }); + context("identifyByUserId", function() { + var uuidV4Spy + before(function() { + uuidV4Spy = sinon.spy(uuid, 'v4') + }) + after(function() { + uuidV4Spy.restore() + }) + afterEach(function() { + uuidV4Spy.reset() + }) + it("should not genereate cid if uid exists and identifyByUserId is true", function() { + var options = { + tid: "UA-XXXXX-XX", + uid: "some-user-id", + identifyByUserId: true + }; + + var visitor = ua(options); + uuidV4Spy.calledOnce.should.equal(false); + visitor.should.not.have.property("cid") + }); + + it("should still generate new cid (UUID) if identifyByUserId is true but uid does not exist", function () { + var options = { + tid: "UA-XXXXX-XX", + identifyByUserId: true + }; + + var visitor = ua(options); + uuidV4Spy.calledOnce.should.equal(true, "uuid.v4 should've been called once"); + utils.isUuid(visitor.cid).should.equal(true, "A valid random UUID should have been generated") + }); + }) + describe("params", function () { From 933134c1569ddd9c48c67c334ae3fb0901f74fb8 Mon Sep 17 00:00:00 2001 From: Putt Date: Tue, 25 Dec 2018 20:46:18 +0700 Subject: [PATCH 2/3] add the readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 5fced20..72bdd35 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,11 @@ If you want to set User Id you can add it into options: ```javascript var visitor = ua('UA-XXXX-XX', {uid: 'as8eknlll'}); ``` +If you want `universal-analytics` to only use `uid` to identify user and using the clientID, just include the otpions `identifyByUserId: true`. This is usedful for those who mainly track user base on UserId. +```javascript +var visitor = ua('UA-XXXX-XX', {uid: 'as8eknlll', identifyByUserId: true}); +``` + [see about User Id](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uid) From 09a44436ea48b67d627c5cb7a4adff5a3cd905bc Mon Sep 17 00:00:00 2001 From: Putt Date: Tue, 25 Dec 2018 21:15:06 +0700 Subject: [PATCH 3/3] fix typo and update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 72bdd35..e6c3076 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ If you want to set User Id you can add it into options: ```javascript var visitor = ua('UA-XXXX-XX', {uid: 'as8eknlll'}); ``` -If you want `universal-analytics` to only use `uid` to identify user and using the clientID, just include the otpions `identifyByUserId: true`. This is usedful for those who mainly track user base on UserId. +If you want to only use `uid` to identify user and not using the clientID, just include the options `identifyByUserId: true`. This is useful for those who mainly track user base on UserId. ```javascript var visitor = ua('UA-XXXX-XX', {uid: 'as8eknlll', identifyByUserId: true}); ```