Created
January 21, 2015 11:28
-
-
Save kzap/1078aa956b8a348b511c to your computer and use it in GitHub Desktop.
Code for doing Register Once traits using Segment.com using a Cookie, this mimics the registerOnce function of MixPanel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _optly = { | |
updateQueryString: function(key, value, url) { | |
if (!url) url = window.location.href; | |
var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)(.*)", "gi"); | |
if (re.test(url)) { | |
if (typeof value !== 'undefined' && value !== null) | |
return url.replace(re, '$1' + key + "=" + value + '$2$3'); | |
else { | |
var hash = url.split('#'); | |
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, ''); | |
if (typeof hash[1] !== 'undefined' && hash[1] !== null) | |
url += '#' + hash[1]; | |
return url; | |
} | |
} | |
else { | |
if (typeof value !== 'undefined' && value !== null) { | |
var separator = url.indexOf('?') !== -1 ? '&' : '?', | |
hash = url.split('#'); | |
url = hash[0] + separator + key + '=' + value; | |
if (typeof hash[1] !== 'undefined' && hash[1] !== null) | |
url += '#' + hash[1]; | |
return url; | |
} | |
else | |
return url; | |
} | |
}, | |
setCookie: function(c_name, value, exdays) { | |
var exdate=new Date(); | |
exdate.setDate(exdate.getDate() + exdays); | |
var c_value=escape(value) + | |
((exdays==null) ? "" : ("; expires="+exdate.toUTCString())); | |
document.cookie=c_name + "=" + c_value; | |
}, | |
getCookie: function(name) { | |
var parts = document.cookie.split(name + "="); | |
if (parts.length == 2) return parts.pop().split(";").shift(); | |
}, | |
delCookie: function(c_name) { | |
var exdate=new Date(); | |
exdate.setDate(exdate.getDate() - 365); | |
var c_value=" ; expires="+exdate.toUTCString(); | |
document.cookie=c_name + "=" + c_value; | |
}, | |
getParameterByName: function(name) { | |
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
results = regex.exec(location.search); | |
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
} | |
window.analytics.ready(function() { | |
// set configs | |
mixpanel.set_config({ | |
"track_links_timeout": 750 | |
}); | |
}); | |
// identify global page variables | |
var identifyVars = { | |
"Page": window.location.href, | |
"Page Title": "Your Page Name", | |
}; | |
// register one time variables, check cookie | |
if (_optly.getCookie('fealty_segment_registeronce') != 1) { | |
identifyVars["Landing Page"] = window.location.href; | |
identifyVars["Landing Page Title"] = "Your Page Name"; | |
// save cookie for 365 days | |
_optly.setCookie('fealty_segment_registeronce', 1, 365); | |
} | |
// save traits using .identify | |
window.analytics.identify(identifyVars); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment