I have a page like for example: http://www.st开发者_如何学JAVAackoverflow.com/how-many-users-visited
I have inside that page already those google analytics javascript under my UA-0000-1 account.
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxx-1']); _gaq.push(['_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); })();
- Using www.google.com/analytics website tools i can see how many visitors came. To my specific url.
Now question:
I wan to use PHP or Javascript to do that query from my own to find those information and render it to my own page.
How can i get all the pages from google analytics using my account? Please kindly show some tips and example, this i tried but does not work.
ex: http://gdata-jsguide.appspot.com/static/analytics/samples/retrievedatacontent.js
To make sure i understand your question: you want to be able to get totals for pages viewed and unique visitors, but you want to store those data yourself rather than retrieve them from the GA server.
So this just requires finding out how GA records for each page and then retrieving those values.
Total pages viewed is easy--for a given page, just record each time _trackPageview() is called and take the sum.
Total unique visitors is also easy but requires a few more steps.
First, you need to know where GA holds this data.
This is an exemplary request header for the tracking pixel (__utm.gif):
http://www.google-analytics.com/__utm.gif?utmwv=4&utmn=769876874&utmhn=example.com&utmcs=ISO-8859-1&utmsr=1280x1024&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=9.0%20%20r115&utmcn=1&utmdt=GATC012%20setting%20variables&utmhid=2059107202&utmr=0&utmp=/auto/GATC012.html?utm_source=www.gatc012.org&utm_campaign=campaign+gatc012&utm_term=keywords+gatc012&utm_content=content+gatc012&utm_medium=medium+gatc012&utmac=UA-30138-1&utmcc=__utma%3D97315849.1774621898.1207701397.1207701397.1207701397.1%3B...
The utmcc portion of this header contains the GA cookies. Within utmcc, is utma which is the Visitor Cookie:
This cookie is comprised of six fields, each delimited by a '.'
97315849.1774621898.1207701397.1207701397.1207701397.1
These fields are:
Domain Hash (discard)
Visitor ID (from a random number generator) this is the piece we want
Initial Visit (a UNIX time stamp)
Previous Session (also a UNIX time stamp)
Current Session (also a UNIX time stamp)
total number of visits (to pages having that GA Account number)
So when a user visits a GA-tagged Site, GA looks for the cookies, which it updates; if GA doesn't find them, it sets them.
The second field in the utmaa cookie is the most important and the one that you want to grab. This is the 'Visitor ID' and it is a random number generated by GA, as a unique ID for each visitor. This cookie persists across sessions and expires two years from date set.
This ID is not viewable in the GA Web Viewer, which means you need to capture it another way. Probably the easiest way is to just send it write a small javascript function to grab this string and bind it to a variable, and then you can just do what you want with it (bind it to a GA custom variable, place it temporarily in a hidden form field, etc.). So for instance:
// takes three parameters: l-> target string; n -> start pattern; s -> end pattern
// returns the visitor cookie (the entire cookie not just the second field)
function vID(l, n, s) {
if (!l || l=="" || !n || n="" || !s || s="") return "-";
var i, i2, i3, c="-";
i=l.indexOf(n);
i3=n.indexOf("=")+1;
if (i > -1) {
i2=l.indexOf(s, i);
if (i2 < 0) {
i2=l.length;
}
c=l.substring((i+i3), i2);
}
return c;
}
var q = vID(document.cookie, '__utma=', ';');
Once you have done this then you will have one visitor ID for each visitor--count them and you get total unique visitors (subject to all of the usual and substantial limitations inherent w/ cookies).
精彩评论