Skip to content

volkanakinpasa/google-analytics-reporting-api-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

google-analytics-reporting-api-wrapper

It covers Google Analytics Reporting API v4 by sending custom variables

Installation

Nuget

  Install-Package GoogleAnalyticsReportingAPIWrapper

Google Analytics

  1. Create a Service Account https://console.developers.google.com/projectselector/iam-admin/serviceaccounts Select Role: Project / Viewer Check: Furnish a new private key / JSON

  2. Attach this file with the (secret) private key to your project.

  3. Enable Google Analytics Reporting API https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview

  4. The service account will have an email. Add that email to your Google Analytics users, preferably only on the specific view you're interested in. (Administration / View / User Management) Read more: https://support.google.com/analytics/answer/1009702?hl=en

Usage

Javascript, send example data

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
ga('send', {
  hitType: 'event',
  eventCategory: 'Product',
  eventAction: 'click',
  eventLabel: 'ProductId'
});

for more info how to send events to Google Analytics click here

to find which dimensions & metrics you need to pass as parameter, please click here

C# get Analytics data

using System;
using System.Collections.Generic;
using Google.Apis.AnalyticsReporting.v4.Data;
using Report = GoogleAnalyticsReportingAPIWrapper.Report;

namespace WrapperConsole
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            List<ReportRow> result = new List<ReportRow>();
            string nextPageToken = null;

            do
            {
                List<DimensionFilterClause> dimensionFilterClauses = new List<DimensionFilterClause>()
                {
                    new DimensionFilterClause()
                    {
                        Filters = new List<DimensionFilter>()
                        {
                            new DimensionFilter()
                            {
                                DimensionName = "ga:eventCategory", Expressions = new List<string>() { "[CategoryName1]", "[CategoryName2]" }, Operator__ = "IN_LIST"
                            }
                        }
                    },
                     new DimensionFilterClause()
                    {
                        Filters = new List<DimensionFilter>()
                        {
                            new DimensionFilter()
                            {
                                DimensionName = "ga:eventLabel", Expressions = new List<string>() { "[LabelName1]", "[LabelName2]" }, Operator__ = "IN_LIST"
                            }
                        }
                    }
                };

                var response = new Report().Get(DateTime.UtcNow.AddDays(-1).ToString("yyyy-MM-dd"),
                    DateTime.UtcNow.ToString("yyyy-MM-dd"), new[] { "ga:eventValue" },
                    new[] { "ga:eventCategory, ga:eventAction, ga:eventLabel" }, "[yourViewId]",
                    "[json file generated by Google API console]",
                    new[] { "https://www.googleapis.com/auth/analytics.readonly" }, "Wrapper app", nextPageToken, dimensionFilterClauses);

                if (response != null && response.Reports.Count > 0 && response.Reports[0].Data.Rows != null)
                {
                    nextPageToken = response.Reports[0].NextPageToken;

                    result.AddRange(response.Reports[0].Data.Rows);
                }
                else
                    nextPageToken = null;
            } while (nextPageToken != null);

        }
    }
}

About

Easy and simple way to connect Google Analytics Reporting API v4

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published