• Stars
    star
    290
  • Rank 142,981 (Top 3 %)
  • Language
    C#
  • License
    Microsoft Public ...
  • Created almost 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GoogleAnalyticsTracker - A C# library for tracking Google Analytics

googleanalyticstracker MyGet Build Status

GoogleAnalyticsTracker

GoogleAnalyticsTracker - A C# library for tracking Google Analytics.

What can it be used for?

GoogleAnalyticsTracker was created to have a means of tracking specific URLs directly from C#. For example, when creating an API using the ASP.NET MVC framework, GoogleAnalyticsTracker enables you to track usage of the API by calling directly into Google Analytics.

Note that for GoogleAnalyticsTracker to work, you should configure Google Analytics as a website. This library will not work when the Google Analytics account is configured as an app.

Get it on NuGet!

Depending on the type of application you are using, use any of the following NuGet packages:

Any application type where framework integration is not needed (Console, WPF, WinForms, ...):

GoogleAnalyticsTracker.Simple

ASP.NET Core:

GoogleAnalyticsTracker.AspNetCore

Example usage - GoogleAnalyticsTracker.Simple

Using GoogleAnalyticsTracker is very straightforward. In your code, add the following structure wherever you want to track page views (note: when using GoogleAnalyticsTracker.Simple, the class to use is SimpleTracker):

using (var tracker = new SimpleTracker("UA-XXXXXX-XX", simpleTrackerEnvironment))
{
    await tracker.TrackPageViewAsync("My API - Create", "api/create");
    await tracker.TrackPageViewAsync("MY API - List", "api/list");
}

Or without a using block:

var tracker = new SimpleTracker("UA-XXXXXX-XX", simpleTrackerEnvironment);
await tracker.TrackPageViewAsync("My API - Create", "api/create");

Example usage - GoogleAnalyticsTracker.AspNetCore

Install the GoogleAnalyticsTracker.AspNetCore package, and register GoogleAnalyticsTracker in your startup class:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // ...

        services.AddGoogleAnalyticsTracker(options =>
        {
            options.TrackerId = "UA-XXXXXX-XX";
            options.ShouldTrackRequestInMiddleware = TrackRequests.Yes;
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseGoogleAnalyticsTracker();
        
        // ...
    }
}

This will automatically track all requests made to your application.

Ideally, you will want to control tracking requests. To do so:

  • Set the options.ShouldTrackRequestInMiddleware = TrackRequests.No; option to disable automatic tracking.
  • Add the [TrackPageView] or [TrackEvent] attributes to your actions.

[TrackPageView] will track a page view in Google Analytics, whereas [TrackEvent] tracks an event.

In case custom tracking is needed, inject an AspNetCoreTracker into your controller and use the tracking methods directly.

Note that the options also provide a couple of events that can be overridden to customize GoogleAnalyticsTracker behaviour:

  • CustomizeTrackerEnvironment lets you customize the tracker environment or inject a custom one.
  • CustomizeAnalyticsSession lets you customize the analytics session or inject a custom one.

Characteristics

GoogleAnalyticsTracker does not track your users. It simply serves as an interface to Google Analytics where you should provide all tracking data that is required. Of course. GoogleAnalyticsTracker sends some data that can be inferred from usage, such as the hostname on which it is running, but not the hostname of your client. Sessions are also untracked: every event that is tracked counts as a new unique visitor to Google Analytics.

  • If you do need to track user sessions, implement a custom IAnalyticsSession and pass it to the constructor of the tracker.
  • If you do need to set common data for all tracking hits, subclass the Tracker or TrackerBase and override the AmendParameters method.

License

MS-PL License

Who uses GoogleAnalyticsTracker?

More Repositories

1

MvcSiteMapProvider

An ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework.
C#
538
star
2

rider-content

A curated list of content around JetBrains Rider
199
star
3

memory-demos

Demos used in various .NET memory management demos on my blog
C#
53
star
4

WindowsAzure.Acs.Oauth2

Oauth2 delegation through Windows Azure Access Control Service for ASP.NET Web API
JavaScript
33
star
5

DotNetWatch

Kotlin
27
star
6

phpunit-runner-teamcity

PHPUnit runner for TeamCity
PHP
24
star
7

dotnetcli-init

.NET CLI tool to initialize NuGet metadata in a .csproj file
C#
20
star
8

SignalR.Node

15
star
9

maartenba.github.io

Maarten Balliauw {blog}
SCSS
12
star
10

teamcity-cloud-azure

Microsoft Azure cloud plugin for TeamCity
Java
11
star
11

BrewBuddy

Sample code for my talk "Brewing Beer with Windows Azure" - http://channel9.msdn.com/Events/aspConf/aspConf/Brewing-Beer-with-Windows-Azure
JavaScript
11
star
12

TwilioTestingMinimalAPI

Source code for Twilio blog article about testing ASP.NET Core Minimal APIs
C#
10
star
13

CamoDotNet

CamoDotNet is all about making insecure assets look secure. This is an SSL image proxy to prevent mixed content warnings on secure pages.
C#
10
star
14

rider-plugin-globaljson

Rider plugin that allows setting the .NET Core SDK version for a solution
Kotlin
9
star
15

ChannelsExample

Example used in blog post about System.Threading.Channels
C#
9
star
16

docker-youtrack

This repository contains a Docker image of JetBrains YouTrack
9
star
17

ChocolateyPackages

Packages I maintain on Chocolatey.org
PowerShell
5
star
18

NuGetTypeSearch

Demo code for NuGetTypeSearch
C#
5
star
19

DotNetContrib.Net.Http.Hsts

HTTP Strict Transport Security (HSTS) support for System.Net.HttpClient, enforcing HTTPS when required by the remote host or preload list.
C#
4
star
20

CustomBindingExample

Custom binding example
C#
3
star
21

BookSomeSpace

BookSomeSpace helps you schedule meetings. It eliminates back and forth emails - people can book straight into your calendar!
JavaScript
3
star
22

BenchmarkWithIndex

Can we optimize https://twitter.com/buhakmeh/status/1291029712458911752 ?
C#
2
star
23

DotNetContrib.AspNetCore.Initialization

Add initialization tasks and an optional splash screen to ASP.NET Core web applications.
C#
2
star
24

OpenProvider.Api

A .NET library to access OpenProvider API's.
C#
2
star
25

AirportDetails

Demo app for EF features in Rider and ReSharper
C#
2
star
26

rider-plugin-reversepackagesearch

Rider example front-end plugin. Perform a reverse NuGet package search via packagesearch.azurewebsites.net.
Kotlin
2
star
27

intellij-ngrok

Bringing ngrok tunnels to the IntelliJ ecosystem
Kotlin
1
star
28

myget-documentation

MyGet Documentation on GitBook
CSS
1
star
29

phpmef

PHPMEF - PHP Managed Extensibility Framework
PHP
1
star
30

XamlStyler.dotUltimate

XAML Styler for Rider (and ReSharper)
C#
1
star
31

rider-plugin-xdt

Rider plugin that allows running configuration transformations (XDT)
Kotlin
1
star
32

kotlin-curious-csharp

Demos for conference talk Kotlin for the Curious C# developer
C#
1
star
33

orchard-authentication-federated

Federated authentication for Orchard. This module enables SAML 2.0 authentication in Orchard, for example Windows Azure AppFabric Access Control Service.
1
star