Cookie Management Recipe for GTM
Add tools to your GTM container to help store and remove cookies. The import will add the functionality to your container. To use the new features, you’ll need to push information to the Data Layer either from your site or a Custom HTML Tag.
Set a cookie by pushing an event to the Data Layer with the following syntax:
dataLayer.push({
'event': 'setCookie',
'attributes': {
'cookieName': 'my_cookie_name',
'cookieValue': 'my_cookie_value',
'cookieDomain': 'www.myhostname.com',
'cookiePath': '/',
'cookieExpires': 1 // Number of days - provide a fraction for periods less than 24 hours
}
});
To remove the same cookie, use the following syntax:
dataLayer.push({
'event': 'removeCookie',
'attributes': {
'cookieName': 'my_cookie_name',
'cookieDomain': 'www.myhostname.com',
'cookiePath': '/',
}
});
Instructions
1. Download Container File
Download the container JSON file.
(You may need to right-click on the link and choose “Save Link As” or “Save Target As” to save the JSON file to your computer.)
2. Import JSON File into GTM
Log into your own Google Tag Manager container and head to the Admin section of the site. Under Container options, select Import Container. Check out this blog post for more details about importing a container file.
3. Preview & Publish
Use the Preview options to test this container on your own site. Try testing each of the events to make sure they’re working properly. If everything looks good, go ahead and publish!