Solved

How to expire Google analytics cookie with php or javascript

Posted on 2012-04-03
14
453 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 82

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
 
LVL 9

Accepted Solution

by:
rinfo earned 500 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now