Monday, 24 February 2014

Acquisition Channels Report using Google Analytics API dotNet


Introduction

Hello everyone,

In this blog, i have analysed the Google Analytic Channels data and according to it i have filtered the available data through the API to form the data for Channels report.

I have fetched the data for Default Channel Grouping labels and its detailed view.

For those who don't know, can find there Channels Report under Acquisition>Channels section. see the Screenshot below:


As we can see Channels Report is new and Google didn't provided its API's. I have analysed data under All Traffic (traffic Source), and filtered that data using filters provided by google(Filter Operation) to get appropriate result.
Use this tool: GA Tool, to analyse data before implementing it in your code.

Code

Here is the sample code, you can also download it here: Samples, put the credentials in your client_secrets.json file.
List of all Metrics and Dimensions are here: Metrics and Dimension

Social:
Fetching Social data, 
Dimension = ga:socialNetwork
Metrics   = ga:visits (change it according to your need)
Filter    = ga:socialNetwork!=(not set);ga:socialNetwork!=(none)

   //Social 
   var request = service.Data.Ga.Get(
       "ga:xxxxxx",   //GA profile id 
       "2014-02-04",  // start date 
       "2014-02-10",  // end date
       "ga:visits"    // metrices
          );
   request.Dimensions = "ga:socialNetwork";
   request.StartIndex = 1;
   request.Filters = "ga:socialNetwork!=(not set);ga:socialNetwork!=(none)";
   request.MaxResults = 10000;
   request.Sort = "-ga:visits";
   GaData results = request.Execute()

Organic Searches:
Fetching Organic data,  
Dimension = ga:sourceMedium,ga:keyword
Metrics = ga:visits (change it according to your need)
Filter  = ga:sourceMedium=@/ organic

//Organic Searches
var request = service.Data.Ga.Get(
       "ga:xxxxxx",   //GA profile id 
       "2014-02-04",  // start date 
       "2014-02-10",  // end date
       "ga:visits"    // metrices
          );

request.Dimensions = "ga:sourceMedium,ga:keyword";
request.StartIndex = 1;
request.Filters = "ga:sourceMedium=@(direct)";
request.Sort = "-ga:visits";
request.MaxResults = 10000;
results = request.Execute();

Direct:
Fetching Direct data,  
Dimension = ga:sourceMedium,ga:pagePath 
Metrics  = ga:visits (change it according to your need)
Filter    = ga:sourceMedium=@(direct)

//Direct
var request = service.Data.Ga.Get(
       "ga:xxxxxx",   //GA profile id 
       "2014-02-04",  // start date 
       "2014-02-10",  // end date
       "ga:visits"    // metrices
          );
request.Dimensions = "ga:sourceMedium,ga:pagePath";
request.StartIndex = 1;
request.Filters = "ga:sourceMedium=@(direct)";
request.Sort = "-ga:visits";
request.MaxResults = 10000;
results = request.Execute();

Email:
Fetching Email data,  Dimension = ga:sourceMedium,ga:pagePath 
Metrics = ga:visits (change it according to your need)
Filter   = ga:sourceMedium=@/ email 

//Email
var request = service.Data.Ga.Get(
       "ga:xxxxxx",   //GA profile id 
       "2014-02-04",  // start date 
       "2014-02-10",  // end date
       "ga:visits"    // metrices
          );
request.Dimensions = "ga:sourceMedium,ga:pagePath";
request.StartIndex = 1;
request.Filters = "ga:sourceMedium=@/ email";
request.Sort = "-ga:visits";
request.MaxResults = 10000;
results = request.Execute();

Other
Fetching Other data,  Dimension = ga:sourceMedium,ga:pagePath 
Metrics = ga:visits(change it according to your need)
Filter = ga:ga:sourceMedium!@(direct);ga:sourceMedium!@/ referral;ga:sourceMedium!@/ email;ga:sourceMedium!@/ organic 

//Other
var request = service.Data.Ga.Get(
       "ga:xxxxxx",   //GA profile id 
       "2014-02-04",  // start date 
       "2014-02-10",  // end date
       "ga:visits"    // metrices
          );
request.Dimensions = "ga:sourceMedium,ga:source";
request.StartIndex = 1;
request.Filters = "ga:sourceMedium!@(direct);ga:sourceMedium!@/ referral;ga:sourceMedium!@/ email;ga:sourceMedium!@/ organic";
request.MaxResults = 10000;
request.Sort = "-ga:visits";
results = request.Execute();

Download sample code Samples.

4 comments: