We begin our August series of integrations with one that has been of significant value to us – and with more and more of our users building second (and subsequent) stores, you may all find value in this, too.
Alfred writes here about the technique he used to extract data from multiple Google Analytics accounts and consolidate them in a single dashboard.
Retrieve Google Analytics Data using the Data Export API.
As all of us are well aware, AspDotNetStorefront has a built-in feature which allows store admin’s to easily integrate with Google Analytics service. If Google Analytics is enabled on your AspDotNetStorefront website, you can pull out your visitor stats, bounce rates, page views, and more using the Data Export API provided by Google. Although you can retrieve them via Data Feed Query Explorer, there isn’t a way you can consolidate data from multiple website profiles and present them for analysis.
After doing a lot of research, I found working code samples, unfortunately though none of them worked the way I wanted them to (retrieve data from different profiles, consolidate, and export to excel). The best thing I could find was this blog post. I used it heavily to create my results.
Using the API requires 3 stage processes:
1. Initialize and Authenticate: Log in via an HTTP POST request and obtain an authorization key.
2. Account Query: Query the stats via an HTTP GET request (passing the key as a header) and obtain a data feed
3. Profile/Report Query: Parse the data feed for display
Initialize and Authenticate:
Pass in your email address and password as the parameters. This part will carry out all the necessary steps to be able to get the data from Google.
This is use to login via an HTTP POST request and obtain an authorization key. If an authorization fails, it will of course throw an exception.
As you can see, the authentication is handled by the Authenticate method. The authentication URL and authentication post for Google are:
The Authenticate URL makes a HTTP POST request which checks if the result has an “Auth=” in it. If so, the user is authenticated and the authentication key is saved into a string variable for further requests.
The GetUserProfiles calls the GetProfilesData method of the BaseClass. It basically sends an HTTP Get Request to “https://www.google.com/analytics/feeds/accounts/default”.
To get the final results, first define what dimensions/metrics you’re interested in and then call the GetAnalytics method.
The GetAnalytics method is responsible for requesting and pulling out the Analytics data. The query string that’s passed on to Google API is much like below:
An important thing to note here is that Google Analytics API does not allow retrieval on multiple profiles on a single request (i.e. https://www.google.com/analytics/feeds/data?ids=ga:xxxx&ids=ga:xxxxx), so you need to loop through the GetAnalytics class. Also, not all dimension/metric combinations are allowed. If you provide an incorrect dimension/metric combination, you’ll get Http 400 (Bad Request) exception. To view the valid combinations, check out this link.
Given your requests are all correct (metrics/dimensions/etc), you’ll see something like below. You’ll also have an option to export it to excel.