Start of Main Content

Many websites use Google Analytics to gather data on-site traffic and user activity. In addition to providing crucial business insights to users through the user console, Google Analytics also has many potential applications in code. For example, a client recently requested a new component for their website that would display the most viewed pages on the site, excluding internal traffic. This client was using Google Analytics on their website, so all the data they were requesting was already available and just needed to be accessed. 

There are several steps that must be taken to set up the Google Analytics Reporting API so that it can be accessed in code:

Enable the Analytics API

  • Once the project is created, go to the APIs and Services section from the main menu

  • Click “Enable APIs and Services;” search for Google Analytics Reporting API and click Enable.

Create a Service Account to access the API

Full documentation can be found on developers.google.com, but here is a summary of the setup steps:

  • Go to IAM & Admin from the main menu
  • Click on Service Accounts and then click Create Service Account
  • Choose a name for your account and for Role select Project > Owner. Check off “Furnish a new private key” and select Key type: JSON
  •  Click Create. A json file with your public/private key pair will be generated and downloaded to your machine; you will need this later in your code
Google documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount

Give the Service Account Access to Your Google Analytics Account

  • Log into Google Analytics and go to Admin
  • Click the account dropdown and select the account/site you would like to access
  • Under the Account column, click on “User Management”
  • Click “+” to add a new user and enter the email address that was generated for the service account you created (e.g. [email protected])

Now the Fun Part

Now that the setup is done, it’s time to start coding. I will provide and explain a code example, with two preliminary steps that must be done in your project.

  1. Add the key file that was downloaded previously to the root level of your project. Rename it something easier to read, such as “service_account_key.json”
  2. Use NuGet to install Google.Apis.AnalyticsReporting.v4

The following example queries Google Analytics to get the most viewed pages on the site; it retrieves the top 1000 pages ordered by page views, descending.

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Web;

using Google.Apis.AnalyticsReporting.v4;

using Google.Apis.AnalyticsReporting.v4.Data;

using Google.Apis.Auth.OAuth2;

using Google.Apis.Services;

using Newtonsoft.Json;

using Sitecore.Diagnostics;



namespace Project.Library.Util

{

    class GoogleAnalyticsUtil

    {

        private string ViewId = "ga:1234567";



        public List GetReport(DateTime startDate, string site)

        {

            List pages = new List();

            try

            {

                string keyFilePath = HttpRuntime.AppDomainAppPath + "service_account_key.json";

                string json = System.IO.File.ReadAllText(keyFilePath);



                var cr = JsonConvert.DeserializeObject(json);



                var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.client_email)

                {

                    Scopes = new[] {

                    AnalyticsReportingService.Scope.Analytics

                }

                }.FromPrivateKey(cr.private_key));



                using (var svc = new AnalyticsReportingService(

                    new BaseClientService.Initializer

                    {

                        HttpClientInitializer = xCred,

                        ApplicationName = "My Site"

                    })

                )

                {

                    // Create the DateRange object.

                    DateRange dateRange = new DateRange()

                    {

                        StartDate = startDate.ToString("yyyy-MM-dd"),

                        EndDate = DateTime.Now.ToString("yyyy-MM-dd")

                    };



                    // Create the Metrics object.

                    Metric pageviews = new Metric {Expression = "ga:pageviews", Alias = "Pageviews"};



                    //Create the Dimensions object.

                    Dimension path = new Dimension { Name = "ga:pagePath" };



                    OrderBy orderby = new OrderBy {FieldName = "ga:pageviews", SortOrder = "DESCENDING"};



                    // Create the ReportRequest object.

                    ReportRequest reportRequest = new ReportRequest

                    {

                        ViewId = ViewId, 

                        DateRanges = new List() { dateRange },

                        Dimensions = new List() { path },

                        Metrics = new List() { pageviews },

                        OrderBys = new List() { orderby }

                    };



                    List requests = new List();

                    requests.Add(reportRequest);



                    // Create the GetReportsRequest object.

                    GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };



                    // Call the batchGet method.

                    GetReportsResponse response = svc.Reports.BatchGet(getReport).Execute();





                    foreach (var report in response.Reports)

                    {

                        var data = report.Data.Rows.Select(x => new AnalyticsPage

                        {

                            Path = x.Dimensions[0],

                            Views = Convert.ToInt32(x.Metrics[0].Values[0])

                        });

                        pages.AddRange(data);

                    }

                }

            }

            catch (Exception ex)

            {

                var st = new StackTrace(ex, true);

                // Get the top stack frame

                var frame = st.GetFrame(0);

                // Get the line number from the stack frame

                var line = frame.GetFileLineNumber();

                Log.Info("Error on line " + line + "; " + ex.Message, this);

            }

            return pages;

        }

    }



    class AnalyticsPage

    {

        public string Path { get; set; }

        public int Views { get; set; }

        public string ViewString { get; set; }

    }



    public class PersonalServiceAccountCred

    {

        public string type { get; set; }

        public string project_id { get; set; }

        public string private_key_id { get; set; }

        public string private_key { get; set; }

        public string client_email { get; set; }

        public string client_id { get; set; }

        public string auth_uri { get; set; }

        public string token_uri { get; set; }

        public string auth_provider_x509_cert_url { get; set; }

        public string client_x509_cert_url { get; set; }

    }

}

 

The first thing being set is the View Id. This can be found in the Google Analytics console. Your account may have multiple Properties and Views; each one has a different ID, and any of these can be used to pull specific data. For example, if your site includes subdomains and you have Google Analytics set up to track traffic on the subdomains separately from the main site, you can use the Id of the subdomain view to get page views for just the subdomain.

Within the GetReport() method, the key file that was placed in the project root is accessed in order to create the credentials for the API, and a new AnalyticsReportingServiceis initialized using those credentials.

In the example above, a start date is provided, which is used to set the date range for the query. There are three other properties added to the request: Metrics, Dimensions, and OrderBys. Metrics and Dimensions specify which fields are returned by the API; Metrics are quantitative, Dimensions are qualitative. For example, page views is a metric; page path is a dimension. A full list of the available Metrics and Dimensions can be found here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets

OrderBy tells the query which field to order on, and in which order. Further documentation on the OrderBy property can be found here: https://www.any-api.com/googleapis_com/analyticsreporting/docs/Definitions/OrderBy

The report request is sent to the API, and the results are transformed into a list of custom Analytics Page objects using Linq.

While this example queries for top pages, there are many other things that the Google Analytics API can be used for. In addition to querying for top pages sitewide, you can query for top landing pages or top exist pages, or you can query the site searches to recommend search keywords or suggest pages that are most commonly searched for. For more query examples, check out the documentation here:

https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries

The Query Explorer is also an excellent tool to build and test queries:

https://ga-dev-tools.appspot.com/query-explorer/

Published:

Latest Ideas

Take advantage of our expertise with your next project.