REST API for Google Analytics
In our last post we showed how Adigami provides a powerful REST API for Google AdWords which doesn't have a REST API. We also support Google Analytics and want to showcase its features in this post.
Let’s continue our “Getting started…” blog series and see how you can easily set up access to a Google Analytics account and start to experiment with the API and the data.
We want to add your Analytics credentials for the client. But since Google Analytics has information about a site (or part of it), let's create the site on Adigami first. You'll see why we need to have a site defined for Adigami in step 3 and beyond.
NOTE: This post assumes you're using our API console on Apigee. If not, just build the URL's we're referring to by prepending https://sandbox.adigami.com to each one.
1. Click on the call that is labeled:
You’ll need to provide just two parameters for this call: the site name and its URL (url).
Once you’re done setting these parameters, go ahead and POST the call. You'll get back a site object and can use the ID in the next call.
2. We're ready to add our Google Analytics credentials. Click on the call that is labeled:
/clients/id/credentials/source (POST)
You'll need to change {id} to your client's ID, and {source} needs to be set to the word analytics. This means your call will probably look something like this:
/clients/23/credentials/analytics (POST)
Finally, you'll need to define just 4 parameters for this call: email, password, profile_id, and site_id. You know what profile_id is in Google Analytics, and site_id refers to the site you just created on Adigami.
You should get a Credential object back that shows the information you provided but the password will be encrypted.
More importantly, Adigami will begin importing two weeks of data for this profile from Google Analytics. If you wait about a few minutes, you should be able to start looking at the data.
3. One way to see if your data is coming in is to check the date range of available data for the site. Use the call labeled below and set the site {id} in the URL:
/sites/id/available-dates (GET)
Once you see some dates come back, you're ready to look at some numbers.
4. To see overall traffic results for the site, use the call labeled below and set the site {id} in the URL:
/sites/id/traffic/all (GET)
You should get something like this:
{ "new_visits": 6980, "pageviews": 21011, "bounce_rate": 0.640350042804147, "bounces": 6732, "entrances": 10513, "pages_per_visit": 1.99857319509179, "visits": 10513, "avg_time": 217.78150860839, "time_on_page": 2289537, "new_visit_rate": 0.663939883953201 }
This is a traffic summary for all the days that we have results for. If you want to limit the date range, simply set one or both of these parameters: start_date, end_date.
The format for dates is YYYY-MM-DD, e.g. 2011-09-26.
The next 3 calls allow you to dive into the traffic results and see performance by medium, by source, and by keyword. There's a lot more you can do beyond these calls and we will be posting more information about them.
4. To see performance by medium, click on the call labeled below, and set the site {id} in the URL:
/sites/id/traffic/mediums (GET)
5. To see just your paid traffic data, do this:
/sites/id/mediums/paid (GET)
6. To see performance of all keywords, do this:
/sites/id/traffic/keywords (GET)
Here're some other questions you can get answers to:
7. Which GA goals were completed on this site:
8. What are the top 10 sites that referred traffic to this site:
/sites/id/traffic/sources?limit=10 (GET)
9. Since September 1, what are the top 3 social media sites that sent traffic to this site:
/sites/id/mediums/social?limit=3&start_date=2011-09-01 (GET)
Google Analytics is a powerful tool and provides a lot of data. What we've tried to do here is capture the 80% -- the most common needs that a user may have into a single easy to understand API so you can be up and running in minutes.
We are constantly improving and adding to our API. Please send us an email with your suggestions.