最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reading Cookie Variable - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share asked Feb 26, 2019 at 20:08 BrettKBrettK 932 silver badges13 bronze badges 5
  • Possible duplicate of Get cookie by name – hindmost Commented Feb 26, 2019 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 Commented Feb 26, 2019 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. <script> 'code' function getCookie(LandingPage) { var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length == 2) return parts.pop().split(";").shift(); } – BrettK Commented Feb 27, 2019 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 e back with the variable. <script> 'code' function getCookie(LandingPage) { var value = "; " + document.cookie; var parts = value.split("; " + LandingPage + "="); if (parts.length == 2) return parts.pop().split(";").shift(); } </script> – BrettK Commented Feb 27, 2019 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 Commented Feb 27, 2019 at 16:04
Add a ment  | 

1 Answer 1

Reset to default 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.

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

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.

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

Performing the click, I get the originally stored value in the console:

发布评论

评论列表(0)

  1. 暂无评论