AdWords
2.9K members online now
2.9K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

Offline conversions - change cookie code to expire after session ends

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

The example code to set cookie described at https://support.google.com/adwords/answer/7012522# is working completely fine, but I need one modification

 

<script type="text/javascript">
function setCookie(name, value, days){
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000)); 
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + ";path=/";
}
function getParam(p){
    var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var gclid = getParam('gclid');
if(gclid){
    var gclsrc=getParam('gclsrc');
    if(!gclsrc || gclsrc.indexOf('aw') !== -1){
	    setCookie('gclid', gclid, 90);
	}
}
</script> 

 The above code makes the cookie to be saved in the browser for 90 days, I want to cookie to saved only until the user is on the site. As soon as the user closes the browser tab/window, the cookie should be deleted.

 

I read somewhere that if I remove the expiry parameters from the above code, it will make the cookie session based but I am not sure what changes I have to make in the above code as not very familiar with cookies.

 

Can someone make the desired changes in the above code so I will be able to set session based cookies on my website. Thank you

 

PS: I can't implement other methods like creating sessions with php due to my website design limitations. I got to work with the above code

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Danish M
June 2016

Re: Offline conversions - change cookie code to expire after session ends

Collaborator ✭ ☆ ☆
# 4
Collaborator ✭ ☆ ☆
Hi
In this line setCookie('gclid', gclid, 90); if you remove the last parameter it will be a session based cookie
the line should look like setCookie('gclid', gclid,);
Regards
Nikhil
Regards, Nik
LinkedIn  |   @nikhilparachure | My Blog

View solution in original post

Re: Offline conversions - change cookie code to expire after session ends

Community Manager
# 2
Community Manager
Hi Danish,

Thanks for posting to the Community. I'm going to bump this up to see if any of our experts are able to help with this, but we often are not able to help with custom code solutions like this, and this may be something you need to reach out to a developer for.

Thanks for your understanding,
Cassie

Re: Offline conversions - change cookie code to expire after session ends

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Thank you Cassie, I understand that it would be difficult to provide a custom code solution

Since it's a code given from Google, I was thinking at least someone from Google explain what parameters are controlling the expiry of cookie in the above code. I know that removing those parameters will make the cookie session based.

I just want to know what are those parameters in the above code so I would remove them myself.
Marked as Best Answer.
Solution
Accepted by topic author Danish M
June 2016

Re: Offline conversions - change cookie code to expire after session ends

Collaborator ✭ ☆ ☆
# 4
Collaborator ✭ ☆ ☆
Hi
In this line setCookie('gclid', gclid, 90); if you remove the last parameter it will be a session based cookie
the line should look like setCookie('gclid', gclid,);
Regards
Nikhil
Regards, Nik
LinkedIn  |   @nikhilparachure | My Blog

Re: Offline conversions - change cookie code to expire after session ends

Collaborator ✭ ☆ ☆
# 5
Collaborator ✭ ☆ ☆

gclid.PNG

 

Here is screenshot of session based cookie after changing the code.

Hope this helps

 

Regards, Nik
LinkedIn  |   @nikhilparachure | My Blog

Re: Offline conversions - change cookie code to expire after session ends

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭
Thank you Nikhil :-) that's what I was looking for