Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

Store URL parameters in a cookie (problem with code)

first of all, I am a total beginner what comes to JavaScript.

Here is my issue: I am trying to create a script + cookie that stores the URL parameters of the website visitor. I am using Google Tag Manager to run the script, so all that I have left to do is finalize the code.

I've been working on this for a few days now and this is how far I've been able to get (the code below).

var cookieName = test-4
function SetCookieValue(cookieValue) {
        var url = window.location.href;
        if(url.indexOf('test-4=') !== -1)
            document.cookie="test-4";
    }  

var expirationTime = 2592000; // One month in seconds
expirationTime = expirationTime * 1000; // Converts expirationtime to milliseconds
var date = new Date(); 
var dateTimeNow = date.getTime(); 

date.setTime(dateTimeNow + expirationTime); // Sets expiration time (Time now + one month)
var date = date.toUTCString(); // Converts milliseconds to UTC time string
function SetCookie(accountCookie) {
   document.cookie= cookieName+"="+cookieValue+"; SameSite=None; Secure; expires="+date+"; path=/; domain=." + location.hostname.replace(/^www./i, ""); // Sets cookie for all subdomains 
 }

Currently, with this code no cookie is injected when the code is run when the Google Tag Manager tag is triggered.

To my understanding, the problem lies in either the first 5 lines or the last 4 lines, as the cookie expiration works fine when used separately (this code is made by combining two codes I found on cookie injection).

Any ideas to get me going again?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...