I'm trying to get some data from our google analytics instance and I'd like to use the Analytics Reporting API V4 Client Library for .NET (https://developers.google.com/api-client-library/dotnet/apis/analyticsreporting/v4) so that I can bake some of this data into an administration site we have built. I'm having trouble finding any examples of using this code and the documentation seems to be incredibly sparse. I would like to use a service account to authorize as we only need to view data associated with the analytics account which we control.
If anyone could provide some sample code or point me in the right direction to get some basic report data using the .net api, it would be greatly appreciated
Calling the Google Analytics Reporting API from C# is not particularly difficult, however all of the necessary steps do not seem to be outlined very clearly in the Google Analyics API documentation. I will try to list them all out here. While YMMV, I believe these steps to be correct as of 7/20/2016.
You can start by creating a new C# project. We'll make a console application to test called
GoogleAnalyticsApiConsole
. Once the project is created, we'll add a reference to the Google Analytics Reporting API V4 Client Library for .NET using the NuGet Package Manager Console (found under the Tools menu in Visual Studio 2015). Fire up the Console and issue the following command at thePM>
prompt:PM> Install-Package Google.Apis.AnalyticsReporting.v4
Installing that package will download the client libraries needed to call the Google Analytics Reporting web services along with a number of other dependencies.
In order to call the web services, you'll need to set up OAuth 2.0 access for your application. The documentation for this setup can be found here, but I will summarize below:
Login to the Google Cloud Platform Console: https://console.cloud.google.com/. Be sure to login with an account that has access to the Google Analytics accounts you are trying to query with the reporting API.
Click the Google Cloud Platform menu and select API Manager.
On the left hand side, click Credentials and then create a new project called
Google Analytics API Console
. Give it some time to create the new project.After the project is created, click Credentials again if it is not already selected, and then click the OAuth Consent Screen link in the right panel. Set the Product name shown to users to
Google Analytics API Console
and then click Save.Click Credentials again, and then click Create Credentials, and choose OAuth Client ID. Select Other for Application type and then enter
Google Analytics API Console
as the Name and click Create.After the credential is created, you will be presented with a client ID and a client secret. You can close the dialog window.
Now, under Credentials you should see an entry under OAuth 2.0 client ids. Click the download icon to the far right of that entry to download the
client_secret.json
file (this file will have a much longer name). Add that file to your project at the root level once it has been downloaded and rename it toclient_secret.json
.Reporting
in the search box and select Analytics Reporting API V4 from the list. On the next screen, click Enable. Once this API has been enabled, you should be able to see it under the Enabled APIs list in the right panel.Now that we've created our project and created our OAuth 2.0 credential, it is time to call the Reporting API V4. The code listed below will use the Google API and the
client_secret.json
file to create aGoogle.Apis.Auth.OAuth2.UserCredential
to query the Reporting API for all sessions between the given date range for a View. The code is adapted from the Java example here.Before executing the code, be sure to set the Build Action on the
client_secret.json
file to Content and the Copy to Output Directory setting to Copy if newer. There are also two variables that need to be properly set. First, in theGetCredential()
method, set theloginEmailAddress
value to the email address used to create the OAuth 2.0 credential. Then, in theMain
method, be sure to set theViewId
in thereportRequest
variable to the view that you want to query using the Reporting API. To find theViewId
, log in to Google Analytics and select the Admin tab. From there, select the view you want to query in the View dropdown on the far right and select View Settings. The View ID will be displayed under Basic Settings.The first time the code is executed, it will bring up a web page asking if you want to allow the
Google Analytics API Console
to have access to the API data. Select Allow to proceed. From then on that permission will be stored in theGoogleAnalyticsApiConsole
FileDataStore
. If that file is deleted, then permission will need to be granted again. That file can be found in the%APPDATA%\GoogleAnalyicsApiConsole
directory.Please note that I beleive this scenario will meet the needs of the OP. If this application were to be distributed to clients, then a different OAuth 2.0 scheme would most likely be necessary.
Here is the code:
I had the same experience: Google's documentation is pretty in-depth but is pretty terrible at giving clear examples of how to connect with .NET.
One key thing I finally realized is that you can either connect using an OAuth2 credential or a service account credential. If you own your Analytics account, use a service account. If you're needing to connect to other users' Analytics accounts, use OAuth2.
There seem to be quite a few examples online of how to get Analytics API data using an OAuth2 credential, but I own my Analytics account and just wanted to pull data from it. I figured out how to connect to the Analytics Reporting API v4 using a
ServiceAccountCredential
, and I wrote an answer on a similar Stack Overflow question with all the details.