How to expire Google analytics cookie with php or javascript


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

Thanks Heaps
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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.
udara22Author Commented:
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?
udara22Author Commented:
Actually this link says you can, but not sure how to ..

Session Cookie Time out
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bernard S.CTOCommented:
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
udara22Author Commented:
@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?
udara22Author Commented:
@rinfo this is my code

    var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-111111-27']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    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?
Try This
var _gaq = _gaq || [];
             ['pageTracker1 ._setAccount', 'UA-111111-27'],
             ['pageTracker1 ._trackPageview'],
             ['pageTracker2 ._setAccount', 'UA-111111-32'],
           ['pageTracker2 ._setSessionCookieTimeout',5],
             ['pageTracker2 ._trackPageview']
udara22Author Commented:
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?
Bernard S.CTOCommented:
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
udara22Author Commented:
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.
udara22Author Commented:
Hi, when I print $COOKIES it gives me

    [__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.

udara22Author Commented:
udara22Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.