Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to expire Google analytics cookie with php or javascript

Posted on 2012-04-03
14
Medium Priority
?
474 Views
Last Modified: 2012-08-13
Hi,

I would like to know how to expire google analytics (ga) __utmz cookie in lets say, 5 seconds not in 6 months?

Thanks Heaps
0
Comment
Question by:udara22
  • 9
  • 2
  • 2
  • +1
14 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37804439
You can't expire someone else's cookie.  You can delete it in your own browser but you do not have access to it in other people's browsers from your web server.  I don't see anything about being able to change the expiration of the Google cookies.  http://code.google.com/apis/analytics/docs/concepts/gaConceptsCookies.html
0
 

Author Comment

by:udara22
ID: 37804450
Thanks Dave, I was thinking, google has set it to expire in 6 months, so though maybe I can set it to expire before that?
0
 

Author Comment

by:udara22
ID: 37804550
Actually this link says you can, but not sure how to ..

Session Cookie Time out
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
rinfo earned 2000 total points
ID: 37805554
With the _setCookeiTimeout you can reduce the cookie expiration time
from 6 months to a value of your choice .
Per day equal to 86400 second
code to be used is

var pageTracker = _gat._getTracker("UA-XXXXXX-X");
    pageTracker._setCookieTimeout("86400"); // will expire cookie in 1 days = 86400 sec.
    pageTracker._trackPageView();
0
 
LVL 29

Expert Comment

by:fibo
ID: 37821412
Before doing that... why do you want to do it?
This will cause GA to report them as new visitors rather than returning visitors... Not sure if you will not be losing some useful information..

A midway strategy might be to reset the cookies' life to one month or 2 weeks, so that you have some decent stats about frequent visitors
0
 

Author Comment

by:udara22
ID: 37825872
@rinfo Thanks, i'll try it out.

@fibo : The reason I need this because Im doing an experiment. The issue with Google cookie is it expires in 6 months so it doenst give us correct information about each user. For an example, assume userA goes to Google and type "keyword one" and visits my website. Now GA cookie thinkgs the keyword is "keyword one", now if the user goes back to bing, without closing the browser or clearing the cookie and type "keyword two" and visits my site, GA cookie still has "keyword one" stored in its cookie. This doesnt give correct information about each visitor. So Im planing to create my own reporting by expiring the cookie after I grab the data, so If a user visits the site again, the cookie records the new data, not the old one as its expired?

Do you guys think Im on the right track?
0
 

Author Comment

by:udara22
ID: 37826429
@rinfo this is my code

    var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-111111-27']);
  _gaq.push(['_trackPageview']);
   
 _gaq.push(['secondTracker._setAccount','UA-111111-32']);
  _gaq.push(['secondTracker._setCookieTimeout("5")']);
   _gaq.push(['secondTracker._trackPageview']);
 
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

The secondTracker is for my tracking purposes and cookie expiring. How can i setCookieTimeout for secondsTracker?
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 2000 total points
ID: 37826512
Try This
var _gaq = _gaq || [];
      _gaq.push(
             ['pageTracker1 ._setAccount', 'UA-111111-27'],
             ['pageTracker1 ._trackPageview'],
             ['pageTracker2 ._setAccount', 'UA-111111-32'],
           ['pageTracker2 ._setSessionCookieTimeout',5],
             ['pageTracker2 ._trackPageview']
             );
0
 

Author Comment

by:udara22
ID: 37826588
Thanks. How can i use php to just echo the second cookie? .so i recon time out means erase the cookie till the visitor visits the site again?
0
 
LVL 29

Expert Comment

by:fibo
ID: 37826620
Why would you want to act on cookies within JS if your page is php?
The cookie resides on the client machine, so is accessable even if js (and GA) is not loaded, right?

To check what happens, Just place within <head> something like
<?php echo "here the cookies hold <pre>[", print-r($_COOKIES,true),"]</pre> ***<br>"; ?>
and you cand directly work on $_COOKIES
0
 

Author Comment

by:udara22
ID: 37826627
Thanks. Because im working with 2 profiles , I thought js is easier. I'll try out just php and see what happens. Thanks for your quick help.
0
 

Author Comment

by:udara22
ID: 37831294
Hi, when I print $COOKIES it gives me

Array
(
    [__utma] => 46210224.531555335.1333436689.1334112343.1334112484.8
    [CMSSESSID64164e49] => 888356ae824c15f723058c4ce5db785a
    [__utmc] => 46210224
    [__utmb] => 46210224.22.10.1334112484
    [__utmz] => 46210224.1334112484.8.2
)

Open in new window


and when I do

_gaq.push(['secondTracker._setCookieTimeout', 5]);  

Open in new window


it removes _utmz as a whole. So I recon it removes  firstTracker and secondsTracker which is not what I kinda want.

Does anyone know how to timeout the secondTracker "utmz" only? PHP or JS i dont mind.

Thanks
0
 

Author Comment

by:udara22
ID: 37849566
Anyone?
0
 

Author Comment

by:udara22
ID: 37863520
anyone?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Ranking ecommerce websites is a vital process. You need to have a strong SEO (Search Engine Optimization) strategy. If you don’t have one, you are losing out on brand impressions, clicks and sales. Check this guide on how to improve website traffic …
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question