The last six month I have spent my nights building a website for the local bandy team.
( If you don’t know anything about bandy you can take a look here.
http://en.wikipedia.org/wiki/Bandy )
They wanted a visitor counter on the site and I used some code I found on the web to quickly create one. It worked fine for some time, but the problem was that my counter did not show the same visitor number as the counter on Google Analytics.
Ok. Now the question was how to show the Google Analytics visitor number on the website.
I spent some time on the web for a quick solution but found none in asp.net.
But I found this site that had the base code for connecting to a Google Analytics account and getting results back as xml.
http://www.akamarketing.com/blog/103-introducing-google-analytics-api-with-aspnet-c.html
Now I had to solve two problems.
First was how the question was to be asked to get the correct answer.
I tried different combination from this page and checked the result as Xml.
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
It took some time to get it right.
The second problem was that I wanted a solution that I can use on several websites with different Google Analytics account.
I chose to authenticate with ClientLogin, witch means I log in with a normal Google account. The problem is that the bandy site is on a ftp where several people has access.
To make sure no one can get there dirty hands on my real admin Google Account, I created a new Google account on a different mail.
In Google Analytics I added the bandy Site to the new user so the new user can read but not edit.
Now I can use the new user account in my code to log in to Google Analytics and get results back.
Special thanks to Dave Callan for letting me use his code.
visitorCounter.zip (5.48 kb)