views:

591

answers:

3

Hi there,

I'm trying to login directly to Google Analytics. To explain, I have an account system and I'd like when you select an ASP.NET button for instance it re-directs you - via a silent login - to a specified Google Analytics account.

I've looked long and hard at Dave Cullen's ASP.NET library and although I can login 'silently' using HttpWebRequest, I can't then stick the user on that page. I'm having allsorts of dramas with a 'Cannot send a content-body with this verb-type' error too.

Here is the very basic code I have currently based on Dave's library;

 string token = GoogleAnalytics.getSessionTokenClientLogin(username, password);
    NameValueCollection profiles = GoogleAnalytics.getAccountInfo(token, GoogleAnalytics.mode.ClientLogin);

    HttpWebRequest theRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/analytics/settings/?et=reset&hl=en_uk&et=reset&hl=en-US&et=reset&hl=en-GB");
    theRequest.Headers.Add("Authorization: GoogleLogin auth=" + token);
    Stream responseBody = theRequest.GetRequestStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    StreamReader readStream = new StreamReader(responseBody, encode);

My question therefore is; 1. can this be done? and 2. is this even the right way to do this?

Advice welcomed!

A: 

Unless you're willing to implement a proxy server to proxy google analytics, I don't think you're going to be able to do this because you can't assign cookies to the client for another domain.

CptSkippy
A: 

If the auth tokens are stored in cookies you can add the cookies to your ASP.NET response - then host the google page in an IFRAME just by setting the src (no inlining). That IFRAME will "inherit" the cookies from your parent page and the page will think it's authenticated.

will
+4  A: 

I'm not sure what the overall goal of signing someone in to Google Analytics automatically is, but if its just to display some of the data that is in Google Analytics you might want to consider using the Google Data API to pull the information that you want from Google Analytics. You could create a simple dashboard of what they really need to see without giving access to other things in Google Analytics, by logging them in you are most likely giving them access to data and tools that they just don't need?

Check out the API if it doesn't fit your needs maybe provide some more information on the overall goal is of this functionality.

http://code.google.com/apis/analytics/

YetAnotherDeveloper
Thanks for all the comments. I've looked at the API and it seems to be the best route. Thanks.
Chris Laythorpe