Google Analytics API – Part 3

In my last two articles I showed a lot of code. First we made the login process, than we stepped through the Analytics hierarchy. Now it is time to get all necessary data from the API!

Get data from Analytics

Analytics offers a lot of data. For this example, we only want some SEO relevant data, but we also want to make some SEM comparisons, so we will get SEO and SEM data, but request them individually.

SEO/SEM differences

What is the difference between SEO and SEM? SEO is all that is naturally. You do not need to spend money. So all SEO traffic is the sum of users that come to your site, because they clicked an unpaid link (on another website, from a google search request, from social media, …). SEM traffic is all traffic that is generated with advertising. So this clicks are all made by ads.


At the Analytics web tool, you will find two different categories:

  • organic
    this is all traffic that is generated naturally. SEO traffic. You can get it with the channel ‘organic’.
  • paid
    this is all traffic, that is generated with ads. SEM traffic. This category is the sum of different channels. All possible channels are: ‘cpa’, ‘cpc’, ‘cpm’, ‘cpp’, ‘cpv’, ‘ppc’.

You have to know this, because you can define the channel for your search request as param.


Traffic is generated through visits. This is the number of user, that request your web page. You can get the number of visits with the following code:

This function uses some params:

  • $analytics
    This is an instance of our Google AnalyticsService class which we created in part 1. With this class we can do API calls.
  • $from, $to
    This is the timespan for which we want to get data. We need to use a String formatted Datetime object, for example 2013-01-01 00:00:00. If $from and $to are equal, you will get data for that day.
  • $profile_id
    This is the ID of the profile. This filter is also used at the Analytics web tool from Google.
  • $channel
    As I told before, we can set a channel filter to only get SEO or SEM data.

The function returns an array with the result. You have to loop through that array to display the data:

The code shows a call to the getValue function. This function looks as follows:

Bounce rate/Visit time/Clicks/…

The example only shows how to get visits. There is many more data that you can request. You find all information for this at the Analytics API documentation.

Part 1 | Part 2

(Visited 645 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *