Google Analytics MetaData API 1


Please Share

Google Analytics
Have you been trying to connect your website or application to Google Analytics? Would you like to be able to show your users Google Analytics data for your website?   If you are you trying to work with the Google Analytics API in C# .net I might be able to help.   In this tutorial series we will be looking into how to connect to Google Analytics API using OAuth2, as well as a service account. I will show you how to get a list of the users Accounts to display to them from the Google Analytics Management API. Using the Meta-data API you will be able to get a full up to date list of the current available metrics and dimensions to display to your users. Finally we will look at getting data back from Google Analytics by using either the Real-time API or the Core reporting API.

Google Analytics API – Seven part Tutorial Series

  1. Google Analytics API Introduction
  2. Google Analytics API Authentication with C# OAuth2 vs Service Account
  3. Google Analytics Management API with C#Accounts, Web Properties and views(Profiles)
  4. Google Analytics Management API with C# – Advanced (coming soon)
  5. Google Analytics Management API with C# – Upload  (coming soon)
  6. Google Analytics Meta-Data API with C# – Showing current dimensions and metrics
  7. Google Analytics Real-Time API with C# – Whats happening now!
  8. Google Analytics Core Reporting API with C#  – Its all about the data baby!

Project Setup

Make sure your project is at least set to .net 4.0.

Add the following NuGet Package

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

Usings

You will probably need most of these using’s

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Apis.Analytics.v3;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using System.Security.Cryptography.X509Certificates;
using System.IO;

Authentication

This tutorial assumes that you have already read the tutorial about Google Analytics API Authentication with C#. I will not be going into how to create a valid Analytics service if you haven’t created one already please go read that tutorial then come back here.

Google Analytics MetaData API

The Google Analytics  MetaData API is where you will find a list of all the the Dimensions and Metrics currently avaliable for use with the Google Analytics Core Reporting API.   At this time we are not able to get a list of the Dimensions and metrics avaliable for use with the Real-Time API but a feature request has been made.    If you would you like to see the list of all of the dimensions and metrics available for you to use with the Google Analytics Core reporting API? Do you want to display a list in your application showing a list of  Dimensions and Metrics that can be used with the Google Analytics Core Reporting API? Do you want to be sure this list is always up to date with what is available through Google?  With C# .net we can get this information.

The Google Analytics MetaData API can give you that list. This list is always up to date with the newest Dimensions and Metrics. Google even tells you when one of the Dimensions or metrics is being depreciated.

Querying the MetaData API

The metadata API  is one of the easier APIs to use. You make a list request to the MetadataResource.ColumnsResource.ListRequest when you execute this request a Google.Apis.Analytics.v3.Data.Columns object is returned to you. This object contains a list of all of the Dimensions and Metrics that you can use currently with the Core Reporting API. (Note: We are using the service var we got back from our Autentication)

MetadataResource.ColumnsResource.ListRequest request = service.Metadata.Columns.List("ga");
Google.Apis.Analytics.v3.Data.Columns result = request.Execute();

Result now contains a full list of everything.

Attributes

Each MetaData column has a set attributes. A column may not have all of these attributes, but they should have most of them.

"attributeNames": [
                   "replacedBy",
                   "type",
                   "dataType",
                   "group",
                   "status",
                   "uiName",
                   "appUiName",
                   "description",
                   "calculation",
                   "minTemplateIndex",
                   "maxTemplateIndex",
                   "premiumMinTemplateIndex",
                   "premiumMaxTemplateIndex",
                   "allowedInSegments"

Here is how to list the different attributes. Items is a list of all of the MetaData columns returned. Each column then has its set of Attributes. So by specifying the name of the attribute we would like to see the value for, we are then able to display it to our user.

foreach (Column cols in result.Items)
    {
        Console.WriteLine("ApiName :" + cols.Id);
        Console.WriteLine("UIName : " + cols.Attributes["uiName"]);
        Console.WriteLine("Group : " + cols.Attributes["group"]);
        Console.WriteLine("Type : " + cols.Attributes["type"]);
        Console.WriteLine("Description: " + cols.Attributes["description"]);
        Console.WriteLine("");
    }

Using Link

Because of the amount of data returns by this list sometimes use link to make things easier.

Just Groups:
result.Items.Select(a => a.Attributes[“group”]).Distinct().ToList();

Just Dimensions:
result.Items.Where(a => a.Attributes[“type”] == “DIMENSION” ).ToList();

Just Metrics
result.Items.Where(a => a.Attributes[“type”] == “METRIC”).ToList();

Conclusion

You should now understand how to access the Google Analytics MetaData API with an authenticated user. We have looked at how to get a quick list of Dimensions or Metrics and how to then display them.

If you had any problems with this tutorial you can check the a sample project for working with Google Analytics API on GitHub as part of the Google-Dotnet-Samples project. With in that project you will find a helper class for the MetaData API with Google Analytics. DaimtoAnaltyicsMetaDataHelper.cs

Please Share


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

One thought on “Google Analytics MetaData API