Search code examples
javascriptcookiesgoogle-tag-manager

Read all cookies in GTM template without knowing specific cookie names (using sandboxed JS)


My website refers traffic to ecom site's product pages and uses a GTM tag to create a 1st party cookie recording the visit. The cookie name is something like visit_[product sku] e.g. visit_12789723.

I'm creating another GTM template which ecom sites will add as a tag to their sale completion page e.g. when conversion occurs.

At the point of sales conversion I need to read all cookies created when product pages were visited e.g. "visit_*". The sandboxed JS provides an API to read cookies - getCookieValues. The problem is getCookieValues requires that you pass in the name of the cookie and it passes back the value.

Is there a way to get all cookies or to use wildcards with the available GTM js API?


Solution

  • If you are okay with obeying the words of the law rather than the spirit, you can create a Javascript variable "document.cookie" and pass the in via a field to your template. Then you can do some custom parsing to get the individual names and values.

    GTM JS variable to retrieve the whole cookie string