Skip to content

Commit

Permalink
added user merge feature
Browse files Browse the repository at this point in the history
  • Loading branch information
buremba committed Jul 1, 2016
1 parent 40973ba commit 34c3c06
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
27 changes: 14 additions & 13 deletions rakam.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,16 +636,16 @@ Rakam.prototype.setDomain = function (domain) {

Rakam.prototype.setUserId = function (userId) {
try {
// var previousUserId = this.options.userId;
var previousUserId = this.options.userId;
this.options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null;

// if ((this._eventId > 0 && previousUserId === null) || (previousUserId !== null && this.deviceIdCreatedAt !== undefined)) {
// var _this = this;
// this.User()._merge(this.deviceIdCreatedAt, function () {
// _this.deviceIdCreatedAt = undefined;
// _saveCookieData(_this);
// });
// }
if ((this._eventId > 0 && previousUserId === null) || (previousUserId !== null && this.deviceIdCreatedAt !== undefined)) {
var _this = this;
this.User()._merge(previousUserId, this.deviceIdCreatedAt, function () {
_this.deviceIdCreatedAt = undefined;
_saveCookieData(_this);
});
}

_saveCookieData(this);
log('set userId=' + userId);
Expand Down Expand Up @@ -2192,16 +2192,17 @@ User.prototype.set = function (properties, callback) {
return this;
};

User.prototype._merge = function (createdAt, callback) {
User.prototype._merge = function (previousUserId, createdAt, callback) {
new Request(getUrl(this.options) + "/merge", {
api: {
"api_version": API_VERSION,
"api_key": this.options.apiKey
"api_key": this.options.apiKey,
"upload_time": new Date().getTime()
},
anonymous_id: this.options.deviceId,
anonymous_id: previousUserId,
id: this.options.userId,
created_at: createdAt,
merged_at: new Date()
created_at: createdAt.getTime(),
merged_at: new Date().getTime()
}).send(wrapCallback("merge", null, callback));

return this;
Expand Down
4 changes: 2 additions & 2 deletions rakam.min.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/rakam.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,16 @@ Rakam.prototype.setDomain = function (domain) {

Rakam.prototype.setUserId = function (userId) {
try {
// var previousUserId = this.options.userId;
var previousUserId = this.options.userId;
this.options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null;

// if ((this._eventId > 0 && previousUserId === null) || (previousUserId !== null && this.deviceIdCreatedAt !== undefined)) {
// var _this = this;
// this.User()._merge(this.deviceIdCreatedAt, function () {
// _this.deviceIdCreatedAt = undefined;
// _saveCookieData(_this);
// });
// }
if ((this._eventId > 0 && previousUserId === null) || (previousUserId !== null && this.deviceIdCreatedAt !== undefined)) {
var _this = this;
this.User()._merge(previousUserId, this.deviceIdCreatedAt, function () {
_this.deviceIdCreatedAt = undefined;
_saveCookieData(_this);
});
}

_saveCookieData(this);
log('set userId=' + userId);
Expand Down
11 changes: 6 additions & 5 deletions src/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ User.prototype.set = function (properties, callback) {
return this;
};

User.prototype._merge = function (createdAt, callback) {
User.prototype._merge = function (previousUserId, createdAt, callback) {
new Request(getUrl(this.options) + "/merge", {
api: {
"api_version": API_VERSION,
"api_key": this.options.apiKey
"api_key": this.options.apiKey,
"upload_time": new Date().getTime()
},
anonymous_id: this.options.deviceId,
anonymous_id: previousUserId,
id: this.options.userId,
created_at: createdAt,
merged_at: new Date()
created_at: createdAt.getTime(),
merged_at: new Date().getTime()
}).send(wrapCallback("merge", null, callback));

return this;
Expand Down

0 comments on commit 34c3c06

Please sign in to comment.