tswedensky87
tswedensky87

Reputation: 11

Trouble Recognizing Cookie With Tag Manager

I created a WordPress function that will apply a cookie when someone visits the admin side of the website. In Google Tag Manager, I created a variable that watches for the cookie.

When I use preview mode, I see the variable looking for the cookie, but the value is still set to "undefined." I have toyed with this for hours and looked at a few different tutorials. Everything I am doing seems to be right, but I can't get GTM to see the cookie.

Can someone help me understand what I am doing wrong?

Upvotes: 1

Views: 1212

Answers (2)

tswedensky87
tswedensky87

Reputation: 11

I looked at this problem closer after taking some time away from it. I had set the path to the /wp-admin/ page. When I first set up the cookie, I thought the path might be necessary to set the cookie to apply only on admins, but I ended up using current_user_can() to apply to admins. Thanks! I overlooked this detail forever.

Upvotes: 0

Eike Pierstorff
Eike Pierstorff

Reputation: 32780

Hard to say since you share neither your GTM setup nor the code that sets the cookie. But you should check if the cookie has the httponly flag set, in which case you cannot read it via Javascript and thus GTM will not recognize it.

In any case, if you already have a WP function you do not need to rely on JavaScript to read cookies - just add some logic to read the cookie server-side and output a JS snippet that pushes the value to the dataLayer.

Upvotes: 1

Related Questions