Google Analytics API with C# Version 1 3


Please Share

In this tutorial we will be looking into version three of the Core Reporting API client Library.     We will be working with getting the account, web property, goal, and segment information back from the Management API.   As well as getting the list of available metadata back from the MetaData API.   Finally we will be working with the Core reporting api and getting our data back.

Note: I am in the process of breaking this tutorial up,  the reason behind this is to give more information on each of the sections.

  • OAuth2 – I have more extensive tutorial on OAuth2  it explains how to use a Saved Refresh token –  Google Oauth2 C# Note: The tutorial was created using Google Drive API as an example but that can be changed by following the example here.
  • Google Analytics MetaData API and contains a sample project. You can find it here:  Google Analytics MetaData API Tutorial
  • Google Analytics Management API – Account Summaries .   You can find it here Google Analytics Account Summaries
  • Google Analytics Management API – This is to become the new main Management API tutorial,  it explains the uses of the Management API.  It will soon link to a basic and advanced tutorials with code examples. In the mean time it does contain a link to the basic Management API sample project code.   You can find it here Google Analytics Management API – Uses
  • More to come  

Requirements

Create Google Apis app

  1. Create a app in Google Apis Console
  2. APIs Section: Select “Analytics API”.
  3. Registered apps Create a Native app.   Make sure to create the OAuth2
  4. Take a note of your Client ID and Client secret

Visual stuido project

Create a new Winforms project.   If you have NuGet you need to add the following package to your project.

PM> Install-Package Google.Apis.Analytics.v3

Note: You need to have the latest version of NuGet to install these packages.

Oauth2

Oauth2RequestBrowser

In order to access most of Google’s APIs you need to use Open Authentication.     Open Authentication is a way for your users to allow your program to access there data for them.   One thing that I want to note is that you are giving OAuth2 access via your Google Account.  If your browser is logged into your personal account for which you don’t have access to your works Google Analtyics data then you wont be able to retrieve the data.  Your web browser needs to be logged into the Google Account that has access to the data you want to request.  (If you are releasing this application to the public its a good idea to put a note about this some place or you will get support calls.  Been there done that)

There are two ways of requesting user access with Open Authentication.   The first is to use something called FileDataStore this will store the RefreshToken on the PC in a file in the %AppData% directory.   The other way of doing it is to create your own implantation of iDataStore, this will enable you to save the RefreshToken to a database for example.   Below you will find the code for using FileDatastore if you want to do this using a refreshToken that is stored in the database please refer to my Google OAuth2 C# tutorial.

Code Using FileDatastore

private void Form1_Load(object sender, EventArgs e)
 {
 UserCredential credential; 
 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
              new ClientSecrets { ClientId = "YourClientId", ClientSecret = "YourClientSecret" },
              new[] { AnalyticsService.Scope.AnalyticsReadonly },
              "user",
              CancellationToken.None,
              new FileDataStore("Analytics.Auth.Store")).Result; }

When you run this code a new browser window will open asking you to authorize the application. When your users give you access to there account all the information you need to access there data is returned to you. If you are going to save this to the database you would have to save the RefreshToken but we are using FileDatastore so we don’t need to worry about that.   In the picture below you can see what was returned.

RefreshToken

Follow up:   Someone recently posted a question with regard to “user” and what its for.    I haven’t had time to dig around in the source code for the API yet but I have done some testing.    If I send  “Linda” using FileDataStore and authorize the access the next time I load the application I don’t have to authorize it again because the file exists in my %appdata% with my stored refreshToken.  But if I change that to say LindaLawton the next time I load it will ask me to authorize it again.   My guess is this will enable you to send a user login.   If you have more then one user using the same machine they will then each have there own information stored in %appData%.   I need to do some more testing I want to see if I can find the change in %appData% or if the file is simply over written.   Rember this is just a guess after my testing if anyone can shed a light on this please feel free to email me or leave a comment.

Service

In order to access Google Analytic’s we need to create a AnalyticsService all of your requests will be going though this.

AnalyticsService service = new AnalyticsService(
                               new BaseClientService.Initializer() {
                               HttpClientInitializer = credential,
                               ApplicationName = "Analytics API sample", });

Notice how we pass it credential, remember this is our access to there data.    Once you have created a service you are ready to go.

Management API

The management API is where all of the information about what accounts the authenticated user has access to.      Before you can query any analytic’s data back you need to tell the Core Reporting API what view (profile) you want to query the data from.     The way we tell the API what view (profile) we want the information for is to send it the id for that view.     The best way to do this is to give your users a list,  but that is easer said then done.   The Google Analytics account managment system can be a little confusing.    A user has a list of accounts , an account could be websites, mobile apps or blogs.    Each account can have more then one Web Property say different sub-domains of the website for example.   Then each web property can have 1 or more views (profiles) this is the id that we need to find.  Confused yet?    Lets look at the picture i found on Google’s site.

Conceptual Overview

gaManagementModel

Quota

When querying data from the Management API you have a limit of 10,000 queries per day per view (Profile) as well as 10 queries per second per user.       If you don’t tell Google who the user is you are doing the query for then they will all be bunched under the same user account.   This could cause your users to hit the quota limit for that day, once you have hit that limit you wont be able to send any more requests to the API for that day.    When sending any of the queries against the management API I recommend that you add either UserIp to the request or QuotaUser.    No you can’t use this to track your users.   Wouldn’t that have been nice?      QuotaUser can be any string.  ProductLicenceKey,  Date.tostring().        If you want to read more about this you can check my blog post Google Analytics Quotas.

Account List

As I told you at the beginning of this tutorial new Client library makes things a lot simpler us.

ManagementResource.AccountsResource.ListRequest AccountListRequest = service.Management.Accounts.List();
Accounts AccountList = AccountListRequest.Execute();

There you go AccountList.items is a list of Account that the user has access to.    Don’t believe me well here is a screenshot.

GoogleAnaltysv3Account

I have 7 accounts that I have access to that’ts why its on Number 7.      Now that I have an Account Id I can go on and find the Web Profiles for this account.

Web Profile List

Account Daimto = AccountList.Items.Where(a => a.Name.ToLower().Contains("daimto")).First();
ManagementResource.WebpropertiesResource.ListRequest WebPropertyListRequest = service.Management.Webproperties.List(Daimto.Id);
Webproperties WebPropertyList = WebPropertyListRequest.Execute();

The first thing I did here was find the account I was looking for, using a bit of Linq magic.    Then we query the Webproperties.list using that id.    So now we have WebPropertyList.items which is a list of the webproperty for the account Daimto.

GoogleAnaltysv3WebProprty

This account only has one web property.   Now I need that Web property Id in order to get a list of profiles for the web property.

Profile List

Webproperty DaimtoWP = WebPropertyList.Items.Where(a => a.Name.ToLower().Contains("daimto")).First();
ManagementResource.ProfilesResource.ListRequest ProfileListRequest = service.Management.Profiles.List(Daimto.Id,DaimtoWP.Id);
Profiles ProfileList = ProfileListRequest.Execute();

This time I find the WebProperty i want first.    Then I request a Profiles.list using the Account.id and the Webproperty.id.   Now I finally have a list of all the profiles for that account and I can pick which one to use for my Core reporting API requests.

Goals List

Getting a list of goals for each of the profiles is also quite easy.

ManagementResource.GoalsResource.ListRequest GoalListRequest = service.Management.Goals.List(Daimto.Id,DaimtoWP.Id,DaimtoProfile.Id);
Goals GoalList = GoalListRequest.Execute();

Now you have a list of goals for that profile.    If you are going to be querying goal metrics from the core reporting API you will need to know the goal number in order to query them out.

example:  You can’t query the metric ga:goalXXStarts   directly you will need to replace the XX with the number of the goal you got back from the Goals.List

Segments List

ManagementResource.SegmentsResource.ListRequest SegmentListRequest = service.Management.Segments.List();
Segments SegmentList = SegmentListRequest.Execute();

Segments arnt attached to account in anyway they are attached to the current authenticated user.  So you don’t need to send any account information with the query.

Users

You can also manage users with the Managment API but in order to do so you will need to add another scope to your OAuth connection above.

AnalyticsService.Scope.AnalyticsReadonly,AnalyticsService.Scope.AnalyticsManageUsers

Once your user has given you permission you then have access to delete, insert, list, and update users. There are three levels to the User permissions  with in the Managment api.   Account, Property and profile.   A user who has access to the account level automatically has access to the lower levels.

List all users who have access to the account:

ManagementResource.AccountUserLinksResource.ListRequest UsersListRequest = service.Management.AccountUserLinks.List(pAccount.Id);
EntityUserLinks userLinks = UsersListRequest.Execute();

List all users who have access to this Property in this Account:

ManagementResource.WebpropertyUserLinksResource.ListRequest request = service.Management.WebpropertyUserLinks.List(pAccount.Id, pWebProperty.Id);
EntityUserLinks result = request.Execute();

List all of the users who have access to this Profile in this Property:

ManagementResource.ProfileUserLinksResource.ListRequest request = service.Management.ProfileUserLinks.List(pAccount.Id, pWebProperty.Id,pProfile.Id);
EntityUserLinks result = request.Execute();

Note: I will update when i get insert, update and delete to work I’m currently having issues with it.

MetaData API

The metadata API is something fairly new.    It is a list of all the dimensions and metrics that we have access to through the Core Reporting API.     This is really nice it means that you can have an updated list for your users in your application.    Trust me its much better then the days when I had to add them manually and keep track of when they released something new.

MetadataResource.ColumnsResource.ListRequest MetadataListRequest = service.Metadata.Columns.List("ga");
Columns metadataList = MetadataListRequest.Execute();

More easy code there.   This time Service is querying the Metadata and getting a list of columns.     Lets take a look

GoogleAnaltysv3MetaDataItems

MetaDataList.items is a list of all the data.columns there are curently 276 available.   Id is the field that you will be using in the Core Reporting API to make your query.

GoogleAnaltysv3MetaDataAttributes

Each item has Attributes this is where you can see everything from the type (dimension, Metric) to what group its part of to the uiName (a nice name to display to users) as well as the description of the item.  See how useful this API is?

I have a new tutorial that goes exactly into how to use the Google Analytics MetaData API and contains a sample project. You can find it here:  Google Analytics MetaData API Tutorial

Core Reporting API

Now we come to the good part.  Lets see some data.

  public List getData(AnalyticsService service, Profile profile) 
        {

            string NextLink = string.Empty;
            List result = new List();
            try
            {
                DataResource.GaResource.GetRequest request = service.Data.Ga.Get("ga:" + profile.Id, "2013-10-01", "2013-11-02", "ga:visits");
                request.Dimensions = "ga:date";
                request.QuotaUser = "MyQuotaUser";
                request.MaxResults = 10;

                int totalResults = 0;   // holder for the totalResults that the query will return
                int rowcnt = 0;         // Holds the row we are curently on.
                do
                {
                    try
                    {
                       GaData DataList = request.Execute();  // Make the request
                       result.AddRange(DataList.Rows);       // store the Data to return later
                       // hack to get next link stuff
                       totalResults = (!DataList.TotalResults.HasValue) ? 0 : Int32.Parse(DataList.TotalResults.ToString());
                       rowcnt = rowcnt + DataList.Rows.Count;
                       NextLink = DataList.NextLink;                       
                       request.StartIndex = rowcnt + 1 ;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("An error occurred: " + e.Message);
                        totalResults = 0;
                    }
                } while (request.StartIndex <= totalResults);

            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
          return result;
        }

now lets look at this we use  service.Data.Ga.Get to request our information it requires that we pass it a few things:

  • profile id:  make sure you add ga: in front
  • Start Date : Format is yyyy-MM-dd
  • End date: Format is yyyy-MM-dd
  • metrics:  you can add more metrics just separate them with a ,

Once you have done that you need to add the list of dimensions  why we have to do this on a second line i don’t know.  Probably because you are no longer required to send at least one dimension.   You get a GaData list back in it there are columns which are the names of the columns returned basically your dimension and metric names.  Then you have rows which contains the actual data returned.

Change log:

2014-1-31: Made change to use new ClientSecrets. Removes downloading and using the Json client secret file. Cleared up issue with User. Added more screenshots to make things clearer also changed profile to view after the new name change by Google.
2014-02-13: Adding examples of how the List user system works. Working on a sample project now.

Conclusion

You should now understand how to retrieve information from management API about the current authenticated user.  You should also be able to get the most up to date list of Metrics and dimensions from the Metadata API.  Finally you can use the information gained from the Management and the Metadata APIs to retrieve data from the Core Reporting API.

 

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog www.daimto.com. This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.


Leave a comment

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

3 thoughts on “Google Analytics API with C# Version 1

  • Ankit Lalan

    Hello Linda,
    Your posts are simply awesome.
    Can you help me with Google adwords- keyword tool? I need the suggestions and traffic of the suggested keywords integrated to a windows based application using c#.
    Need some idea to integrate API with my project .

    • Linda Lawton
      Linda Lawton Post author

      I am happy to hear you have found my work helpful. I haven’t worked with adwords becouse i dont have access to an adwords account. I would be happy to help anyway i can you can contact me here.

  • Tonny Thiatmaja

    Thank you for the article.

    in your Core Reporting API sample

    the return type should be List<IList> instead of “List”

    public List<IList> getData(AnalyticsService service, Profile profile)
    {
    ….
    }