Download the Full Visual Studio Solution with the Source Code

In this post series, I show you the process of building a prototype for a Google Analytics Extractor to SAP Business Warehouse.
You can replicate the source code by reading the posts. If you want to save time, you can also download the ready-to-use Visual Studio solution file with the source code. The posts explain my code in all the needed details.

Overview

In the previous post,I have prepared a skeleton of the Google Analytics Extractor and a workflow that our code goes through. Now it's time to implement the first step, Initialize Analytics Client.

#

Workflow Step

Method Signature

1

Initialize Analytics Client

AnalyticsService InitializeAnalyticsClient()

2

Execute Query

GaData ExecuteQuery(AnalyticsService analyticsclient)

3

Save Result to CSV

void SaveResultToCSV(GaData data)

As you remember, the expected result of this method that we want to have a working Client with which we can easily execute our query. Getting a working client is the hardest work of our extractor, since it has to go through an Authentication process with OAuth.

But I have a Good News for you, once you can get through this method, the remaining steps are a child's play. Let's dive into this method and first prepare your service for the OAuth process.

Prepare Service Account in Google Developers Console

P12 file: Certificate file name for X509 Certificate. Save it to a secure place.

Password for the Certificate: Google gives "notasecret"

Service Account Id: Service email generated by the Google Developers Console

You can find a 5 step process in the section Creating a service account. All of these steps are necessary for building a secure channel to Google with OAuth, so do it.
Once you have these pieces in your hand, you can refer to them in your application. I have used constants for the sake of the simplicity.

Initializer classes for connection configuration

As I already mentioned, we are ready with our work once we have a configured AnalyticsService class. We can consider the client configured if it can send request to the Google service without having authentication and security issues.
So we need to make our AnalyticsService understand where to find our secured tools that we gathered previously in the Developers Console.

The Client Library uses so-called "Initializer" classes for the configuration and all of them are chained together, all-in-all it's a bit complex:

BaseClientService.Initializer: This is the class that configures our AnalyticsService Client at the end and we need it for the service construction. But it requires a ServiceAccountCredential first.

ServiceAccountCredential.Initializer: This class can create a ServiceAccountCredential for the BaseClientService.Initializer, but it requires to have a configured X509Certificate2 class.

X509Certificate2: This is the deepest element of our construction chain. Here we need to refer to our P12 file, password.

It was overly complex for me for the first sight but let's see how I could put the puzzle pieces together.

Code Snippet: Initialize Analytics Service

Let me explain you my code. I have implemented the InitializeAnalyticsClient method that is a factory method responsible for constructing a configured AnalyticsServiceClient.
This method creates a chain of configuration explained in the previous list, just in the opposite order:

First, it creates a certificate based on the given P12 file and password and stores it in the "certificate" variable.

Then it creates a credential with the the certificate storing it in the "credential" variable and

finally it creates the AnalyticsService by passing the the BaseServiceClient initialized with the credential variable.

As you can see on my code listing, I delegated the construction steps into 2 helper factory methods, each is responsible to construct a simple class to resolve a dependency.

Get Service Account Credential Initializer

I think, by reading this method, it explains itself. It just asks a readonly access to your service account. The account is identified by the service email and authorized with the certificate. If everything is set properly, it will return a credential that can be used for initializing your service.

Get Service Initializer

This is a really simple method. It creates the basic configuration based on your credential and you can pass this configuration to the constructor of your AnalyticsService client.

Summary

That's it! If you found this part - because I found it - a little bit complex, don't worry.
This was the most challenging part of our code, the remaining steps are really easy.
Authenticating is like opening a closed door just you need to find your appropriate key and method to open it. And of course it has to be a little bit complicated since we don't want to have a Google Analytics database that is easy to open for everybody.

I hope, you could unlock your service by using the code above. You are ready to test it! Just run the code and tell me in the comment section if it worked for you! Or maybe you have an easier method to unlock the door of Google Analytics, share it in the comment section, I am curious about your approach!

If you liked my posts about Google Analytics until now and don't want to miss my next updates, just subscribe to my newsletter below this post. I plan to finish the code snippets with an Analytics query, save it to Excel. I also want to automate the compiled code with a Process Chain in BW. Stay tuned.