2

There is a TON of javascript information out there, but I can't seem to put it all together. Through Google Tag Manager I've created a script to create a Cookie. I then used a 1st party variable to capture that value. This looks to be working. In the developer toolbox, in cookies, I can see the cookie with the name "LandingPage" and value of "/XYZ/". There are several other cookie values as well, but I'm focused on this particular one. What I can't figure out is how to read/display that particular "LandingPage" value.

For simplicity of learning, I was just trying to create a function, like an alert, to display the value of "LandingPage", which is "/XYZ/". I just haven't been able to get the LandingPage value to display.

Below is the code I'm using to set/create the cookie.

<script>
 function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
createCookie("LandingPage","{{Page Path}}")
</script>
BrettK
  • 93
  • 2
  • 12
  • Possible duplicate of [Get cookie by name](https://stackoverflow.com/questions/10730362/get-cookie-by-name) – hindmost Feb 26 '19 at 20:12
  • Do I understand well, that you use GTM to set the cookie, and you'd also like to retrieve the value of this same cookie in GTM? – kgrg Feb 26 '19 at 20:43
  • The ultimate goal is to pass this variable not just into Google Analytics, but to pass the variable into a form submission as well @kgrg Yes - This is a similar to 'Get Cookie By Name', but I can't get that specific code to work. – BrettK Feb 27 '19 at 15:14
  • I tried to edit my message but couldn't. Below is what I try to run in the developer console. I assumed it should come back with the variable. – BrettK Feb 27 '19 at 15:24
  • I'm initially just looking for a way to confirm that I have code to pull the correct variable. That is why I wanted to display the variable in something like an alert box. Once I know I can 'read' the variable, I'd move to taking the variable and passing it into Google Analytics and form submissions. – BrettK Feb 27 '19 at 16:04

1 Answers1

4

In Google Tag Manager (GTM) you can easily access 1st party cookies with GTM's built-in variable called "1st Party Cookie". You need to provide a valuable name for your reference, e.g. "Landing Page Cookie Value", and also provide the name of the cookie, which is LandingPage in your case. You can reference this variable in any built-in tags, other variables, or even in custom HTML (script) codes: {{Landing Page Cookie Value}} based on the example name in this post. The variable should contain the value of your cookie.

Edit, detailed explanation

This is exactly the code, copied from the original post. I'm creating a new Custom HTML tag, which is run on all pages during page load. enter image description here

This is my setting to create the variable, referencing the same cookie name.

enter image description here

I have also created a very simple custom HTML tag, which runs on any click event. So by simply clicking anywhere on the page, it'll trigger the code to run. It tries to display the content of the recently created Cookie variable in the console. enter image description here

By opening my test page, after having turned on GTM preview mode, I can see my first tag having run on pageview. enter image description here

Performing the click, I get the originally stored value in the console: enter image description here

kgrg
  • 1,605
  • 1
  • 12
  • 14
  • And this is where I get stuck. It may be what I'm trying to do or not understanding what can be done. I've tried several different codes and I can't get the variable to display in something like an alert function. Do you have a sample code I can try? – BrettK Feb 27 '19 at 22:17
  • Thanks for the update. Please see my edited answer with detailed steps, how I get the value, based on your original code for writing the cookie. – kgrg Feb 28 '19 at 06:37
  • Great explanation and thanks for the taking the time to personalize it for me! It was exactly what I needed. I was able to see in the developer console the landing page value. I marked it as the best answer. – BrettK Feb 28 '19 at 14:32