Skip to content

Commit 81d61d0

Browse files
author
Curtis
committed
Save user id and global user properties to cookie
1 parent 335dfc1 commit 81d61d0

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

amplitude-1.0-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@
515515
apiEndpoint: 'api.amplitude.com',
516516
cookieName: 'amplitude_id',
517517
cookieExpiration: 365 * 10,
518-
unsentKey: 'amplitude_unsent',
519-
gupKey: 'amplitude_gup'
518+
unsentKey: 'amplitude_unsent'
520519
};
521520

522521
var eventId = 0;
@@ -531,34 +530,61 @@
531530
Amplitude.prototype.init = function(apiKey, opt_userId) {
532531
this.options = options;
533532
options.apiKey = apiKey;
534-
options.userId = (opt_userId !== undefined && opt_userId !== null && opt_userId) || null;
533+
534+
// Load cookie data
535+
var cookie = Cookie.get(options.cookieName);
536+
var cookieData = null;
537+
if (cookie) {
538+
try {
539+
cookieData = JSON.parse(LZW.decompress(Base64.decode(cookie)));
540+
if (cookieData) {
541+
if (cookieData.deviceId) {
542+
options.deviceId = cookieData.deviceId;
543+
}
544+
if (cookieData.userId) {
545+
options.userId = cookieData.userId;
546+
}
547+
if (cookieData.globalUserProperties) {
548+
options.globalUserProperties = cookieData.globalUserProperties;
549+
}
550+
}
551+
} catch (e) {
552+
// Do nothing
553+
}
554+
}
555+
556+
options.deviceId = options.deviceId || UUID();
557+
options.userId = (opt_userId !== undefined && opt_userId !== null && opt_userId) || options.userId || null;
558+
saveCookieData();
535559

536560
//log('initialized with apiKey=' + apiKey);
537561
//opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId);
538562
eventId = 0;
539-
if ((options.deviceId = Cookie.get(options.cookieName)) == null) {
540-
options.deviceId = UUID();
541-
Cookie.set(options.cookieName, options.deviceId, options.cookieExpiration);
542-
}
563+
543564
var savedUnsentEventsString = localStorage.getItem(options.unsentKey);
544-
var savedGlobalUserPropertiesString = localStorage.getItem(options.gupKey);
545-
if (savedGlobalUserPropertiesString) {
546-
options.globalUserProperties = JSON.parse(LZW.decompress(Base64.decode(savedGlobalUserPropertiesString)));
547-
}
548565
unsentEvents = (savedUnsentEventsString && JSON.parse(LZW.decompress(Base64.decode(savedUnsentEventsString)))) || [];
549566
if (unsentEvents.length > 0) {
550567
this.sendEvents();
551568
}
552569
};
553570

571+
var saveCookieData = function() {
572+
Cookie.set(options.cookieName, Base64.encode(LZW.compress(JSON.stringify({
573+
deviceId: options.deviceId,
574+
userId: options.userId,
575+
globalUserProperties: options.globalUserProperties
576+
}))), options.cookieExpiration);
577+
};
578+
554579
Amplitude.prototype.setUserId = function(userId) {
555580
options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null;
581+
saveCookieData();
556582
//log('set userId=' + userId);
557583
};
558584

559585
Amplitude.prototype.setGlobalUserProperties = function(globalUserProperties) {
560586
options.globalUserProperties = globalUserProperties;
561-
localStorage.setItem(options.gupKey, Base64.encode(LZW.compress(JSON.stringify(globalUserProperties))));
587+
saveCookieData();
562588
//log('set globalUserProperties=' + JSON.stringify(globalUserProperties));
563589
};
564590

@@ -635,6 +661,8 @@
635661

636662
window.amplitude = instance;
637663
window.Base64 = Base64;
664+
window.Cookie = Cookie;
665+
window.LZW = LZW;
638666

639667
// Apply the queued commands
640668
for (var i = 0; i < q.length; i++) {

0 commit comments

Comments
 (0)