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?
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.
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?
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

udara22Author Commented:
Actually this link says you can, but not sure how to ..

Session Cookie Time out
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:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.