We help IT Professionals succeed at work.

Google Tag Manager Cookie being stripped.

Jon Imms
Jon Imms asked
on
Medium Priority
76 Views
Last Modified: 2020-01-28
Hey there,  We have a cookie, setup in Google Tag Manager, which is supposed to follow a user through the site, and expire after 1 month.  

I.e  user clicks on an add,  goes to https://buildertrend.com/?source=dev-test.  Source Tag fires.
user then goes to any other page on the site, we lose the cookie value. Not sure why it is stripping the value ?


<script>
  
  //Get value from URL parameter "source"
  
 function getParameterByName(name) {
     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
     var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
         results = regex.exec(location.search);
     return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
 }
  
 // Set Cookie
 
 var cookieName = "sourcetag"; // Name of your cookie
 var source = getParameterByName('source'); // Value of your cookie
 var expirationTime = 2592000; // One month in seconds
 expirationTime = expirationTime * 1000; // Converts expirationtime to milliseconds
 var date = new Date(); 
 var dateTimeNow = date.getTime(); 

 date.setTime(dateTimeNow + expirationTime); // Sets expiration time (Time now + one month)
 var date = date.toUTCString(); // Converts milliseconds to UTC time string
 document.cookie = cookieName+"="+source+"; expires="+date+"; path=/; domain=." + location.hostname.replace(/^www\./i, ""); // Sets cookie for all subdomains

</script>

Open in new window

Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
The current version of Firefox is blocking tracking cookies.  Chrome will also do that soon.
Principal Software Engineer
CERTIFIED EXPERT
Commented:
As Dave says.  Many people do not like being tracked and browser authors are supporting this.  The proposed Do Not Track notice from the browser has been largely ignored by web sites; now browsers will become more aggressive about protecting their users.

Firefox, over its lifetime, has had many ways to strip cookies.

Many anti-malware and virus scanners will also perform this service for the browser.

When a browser's data storage is automatically cleared on exit, so is that cookie.

And on my system, the goofle tag manager site is completely blocked twice, both in Adblock Plus and in the hosts file.

If a cookie is not first-party (viz., on your own domain) it is not prudent to rely on it being there when you come back for it.  Certainly don't expect any cookie to persist for a month; expect that it will be good for this session only and deleted when the user shuts off the computer.

BTW:  Firefox is also looking at defeating "browser profiling" and there are already numerous plugins to help with this.

Reject third party cookes

Reject goofle tag manager
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
When debugging this type of problem first step is a determination of exact problem

1) Server problem.

2) Browser problem.

So... Running this command...

curl -I -L 'https://buildertrend.com/?source=dev-test&foo1=1&foo2=2'

Open in new window


Shows no redirect. So your next step will be to place a env.php file on your server. Something like this...

<?php

$array = $_SERVER;
ksort($array);

while (list($var,$value) = each ($array)) {
   $value = trim(preg_replace("/<address>|<\/address>/", '', $value));
   echo $var . " = " . $value . "<br />\n";
}

?>

Open in new window


Then visit the env.php URL. This will verify all query parameters are correct.

If any are stripped you have a server problem. If all are intact, then whatever problem is occurring is browser related, which you have no control over for your visitors.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.