This Google Tag Manager Recipe was originally posted by Bounteous.
It allows you to set or remove cookies via data layer pushes. See the recipe description for details of the information to include in the data layer push.
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': '/', } });
Related Resource
You might also be interested in this guide: How to set and read cookies with Google Tag Manager
INSTRUCTIONS
- Download Container File
Download the container JSON file (right-click on the link and click “Save Link As” or “Save Target As” to save the JSON file to your computer). - 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. Read this blog post for more details about importing a container file. - 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!
View all 30+ Google Tag Manager Recipes