Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to use uid only without cid #122

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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});
```

[see about User Id](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uid)


Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

Expand Down