Hi there, I have recently set up a code in google tag manger to store a cookie based of a query parameter. That cookie is then supposed to be pushed to a user after they submitted a form. The purpose is to figure out where a person came from to get to our site. I’ve set up the following code but still have no success. I’ve also tried placing an email manualy, but it still does not update the user. Please help!
<script>
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
var theref = urlParams["ref"];
document.cookie = "ref="+theref+";max-age=7200";
window.addEventListener("message", function(event) {
if (event.data.type === "hsFormCallback" &&
event.data.eventName === "onFormSubmit" &&
event.origin === document.location.origin) {
setTimeout(function(){
var _hsq = _hsq || [];
_hsq.push(["identify",{
usertoken: getCookie("hubspotutk"),
referrer: getCookie("ref")
}]);
}, 10000);
}
});
</script>