It covers Google Analytics Reporting API v4 by sending custom variables
Install-Package GoogleAnalyticsReportingAPIWrapper
-
Create a Service Account https://console.developers.google.com/projectselector/iam-admin/serviceaccounts Select Role: Project / Viewer Check: Furnish a new private key / JSON
-
Attach this file with the (secret) private key to your project.
-
Enable Google Analytics Reporting API https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview
-
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
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
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);
}
}
}