Skip to content

Commit

Permalink
Merge pull request #114 from abridger/tracking-fixes
Browse files Browse the repository at this point in the history
Update tracking behavior to use localStorage
  • Loading branch information
Paul Varache authored Apr 28, 2017
2 parents a873c22 + a051a1e commit c94e903
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions kano-tracking/kano-tracking-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,31 +162,23 @@
_setSchema () {
// When using Kano Code within the Electron app, we want to be able
// to provide the app's schema in place of the default schema
this.schema = this._parseParams('tracking-schema') || this.context.config.TRACKING.SCHEMA;
this.schema = localStorage.getItem('KANO-TRACKING-SCHEMA') || this.context.config.TRACKING.SCHEMA;
},
_setIds () {
let browserId = localStorage.getItem('KANO-TRACKING-BROWSER-ID'),
sessionId = sessionStorage.getItem('KANO-TRACKING-SESSION-ID'),
savedSessionId = localStorage.getItem('KANO-TRACKING-SESSION-ID'),
urlSessionId = this._parseParams('session-id'),
idString = window.navigator.userAgent + Date.now().toString(),
hashedId = md5(idString);
// If a `session-id` has been provided in the URL, then we always
// want this to take priority
if (urlSessionId) {
sessionId = urlSessionId;
sessionStorage.setItem('KANO-TRACKING-SESSION-ID', urlSessionId);
// If we already have a `sessionId`, then we can assume that
// the session has already been started
this.previousSession = true;
}
// If a `session-id` has been saved in localStorage, then we want
// make use of this to continue the session, and then clear
// localStorage
if (savedSessionId) {
sessionId = savedSessionId;
sessionStorage.setItem('KANO-TRACKING-SESSION-ID', savedSessionId);
localStorage.removeItem('KANO-TRACKING-SESSION-ID');
if (!this.preserveSavedSession) {
localStorage.removeItem('KANO-TRACKING-SESSION-ID');
}
this.previousSession = true;
}
if (!browserId) {
Expand All @@ -209,22 +201,12 @@
return 'Unknown';
});
},
_parseParams (name) {
let qs = window.location.search,
regex,
result;
if (qs.length > 1) {
regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
result = regex.exec(qs);
if (result && result[2]) {
return decodeURIComponent(result[2].replace(/\+/g, ' '));
}
}
return null;
},
_saveSession () {
localStorage.setItem('KANO-TRACKING-SESSION-ID', this.sessionId);
},
_saveSchema () {
localStorage.setItem('KANO-TRACKING-SCHEMA', this.schema);
},
_sessionExpired (lastUpdate) {
if (!lastUpdate) {
return false;
Expand Down

0 comments on commit c94e903

Please sign in to comment.