# maartenba/googleanalyticstracker architecture

> Architecture diagram of the GitHub repository maartenba/googleanalyticstracker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maartenba/googleanalyticstracker
- Repository: https://github.com/maartenba/googleanalyticstracker

## Overview

##### Project type and purpose
- This is a multi-project .NET solution containing:
- A core C# library (`GoogleAnalyticsTracker.Core`) that implements the tracking logic and parameter models
- Two integration libraries:
– `GoogleAnalyticsTracker.Simple` for console/WPF/WinForms
– `GoogleAnalyticsTracker.AspNetCore` to hook into the ASP.NET Core pipeline via middleware and attributes
- A sample ASP.NET Core MVC web application (`GoogleAnalyticsTracker.SampleWebApplication`) demonstrating how to use the AspNetCore package
- A Nuke-based build script under `build/` and a solution file to orchestrate building, testing, and packaging
- Purpose: Provide reusable, framework-agnostic tracking logic plus framework‐specific extensions so C# applications can send page-view and event hits to Google Analytics.

##### File structure
- Top-level projects and their roles:
- `GoogleAnalyticsTracker.Core/` – tracking engine, data models, HTTP request wrappers, interfaces for session/environment customization
- `GoogleAnalyticsTracker.Simple/` – concrete `SimpleTracker` and environment for standalone apps
- `GoogleAnalyticsTracker.AspNetCore/` – middleware (`UseGoogleAnalyticsTracker`), DI registration, controller attributes (`[TrackPageView]`, `[TrackEvent]`)
- `GoogleAnalyticsTracker.SampleWebApplication/` – ASP.NET Core MVC app showing controller usage, views, static assets, appsettings
- `build/` – Nuke build definition to compile all projects, run tests, produce NuGet packages
- Solution and global config files manage versions and cross-project settings
- Key patterns: layered modular libraries, attribute-based AOP for request tracking, middleware pipeline, dependency injection.

##### README for additional insights
- The README describes usage scenarios for each package variant and configuration options
- It notes the customizable extension points (`IAnalyticsSession`, `ITrackerEnvironment`, override methods)
- It describes how the AspNetCore package hooks into `Startup.ConfigureServices` and `Configure`

##### Elements
- From SampleWebApplication to AspNetCore package (runtime reference)
- From AspNetCore/Simple to Core (compile and runtime dependency)
- From Core to External HTTP Endpoint (runtime data flow)
- From Build Script to each project (build-time action)

##### Be detailed and capture essential architectural elements
- How customization hooks (`IAnalyticsSession`, `ITrackerEnvironment`) sit in Core and are surfaced by the adapters.
- Separation of concerns: the Core library has no web dependencies, the AspNetCore adapter only wires Core into ASP.NET Core.
- Both automatic tracking (middleware) and manual tracking (method calls in controllers or console code).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build Pipeline
    NukeBuild["Nuke Build Script"]:::build
    click NukeBuild "https://github.com/maartenba/googleanalyticstracker/tree/master/build/"

    %% Core Library
    Core["GoogleAnalyticsTracker.Core"]:::core
    click Core "https://github.com/maartenba/googleanalyticstracker/tree/master/GoogleAnalyticsTracker.Core/"

    %% Adapter Libraries
    Simple["GoogleAnalyticsTracker.Simple"]:::adapter
    click Simple "https://github.com/maartenba/googleanalyticstracker/tree/master/GoogleAnalyticsTracker.Simple/"
    AspNetCore["GoogleAnalyticsTracker.AspNetCore"]:::adapter
    click AspNetCore "https://github.com/maartenba/googleanalyticstracker/tree/master/GoogleAnalyticsTracker.AspNetCore/"

    %% Sample Application
    SampleApp["GoogleAnalyticsTracker.SampleWebApplication"]:::app
    click SampleApp "https://github.com/maartenba/googleanalyticstracker/tree/master/GoogleAnalyticsTracker.SampleWebApplication/"

    %% External Service
    GAEndpoint["Google Analytics Endpoint"]:::external

    %% Relationships
    NukeBuild -->|builds| Core
    NukeBuild -->|builds| Simple
    NukeBuild -->|builds| AspNetCore
    NukeBuild -->|builds| SampleApp

    Simple -->|depends on| Core
    AspNetCore -->|depends on| Core
    SampleApp -->|references| AspNetCore

    Core -->|sends hits via HTTP| GAEndpoint

    %% Class Definitions
    classDef core fill:#f9f,stroke:#333,stroke-width:1px
    classDef adapter fill:#ff9,stroke:#333,stroke-width:1px
    classDef app fill:#9f9,stroke:#333,stroke-width:1px
    classDef build fill:#9ff,stroke:#333,stroke-width:1px
    classDef external fill:#ccc,stroke:#333,stroke-width:1px
    

    %% Core Subcomponents
    subgraph "Core Library Components"
        direction TB
        TrackerBase["TrackerBase"]:::core
        IAnalyticsSession["IAnalyticsSession"]:::core
        ITrackerEnvironment["ITrackerEnvironment"]:::core
        TrackerParameters["TrackerParameters"]:::core
        HttpClientWrapper["HttpClientWrapper"]:::core
    end
    Core --> TrackerBase
    Core --> IAnalyticsSession
    Core --> ITrackerEnvironment
    Core --> TrackerParameters
    Core --> HttpClientWrapper

    %% Simple Adapter Subcomponents
    subgraph "Simple Adapter Components"
        direction TB
        SimpleTracker["SimpleTracker"]:::adapter
        SimpleEnv["SimpleTrackerEnvironment"]:::adapter
        SimplePageExt["PageViewTrackerExtensions"]:::adapter
        SimpleEventExt["EventViewTrackerExtensions"]:::adapter
        SimpleScreenExt["ScreenviewTrackerExtensions"]:::adapter
    end
    Simple --> SimpleTracker
    Simple --> SimpleEnv
    Simple --> SimplePageExt
    Simple --> SimpleEventExt
    Simple --> SimpleScreenExt

    %% AspNetCore Adapter Subcomponents
    subgraph "AspNetCore Adapter Components"
        direction TB
        Middleware["GoogleAnalyticsTrackerMiddleware"]:::adapter
        TrackerSvc["AspNetCoreTracker"]:::adapter
        Extensions["AspNetCoreTrackerExtensions"]:::adapter
        CookieSession["CookieBasedAnalyticsSession"]:::adapter
        AttrPage["TrackPageViewAttribute"]:::adapter
        AttrEvent["TrackEventAttribute"]:::adapter
        TrackRequests["TrackRequests"]:::adapter
        Options["GoogleAnalyticsTrackerOptions"]:::adapter
        ApiExtensions["GoogleAnalyticsTrackerExtensions"]:::adapter
    end
    AspNetCore --> Middleware
    AspNetCore --> TrackerSvc
    AspNetCore --> Extensions
    AspNetCore --> CookieSession
    AspNetCore --> AttrPage
    AspNetCore --> AttrEvent
    AspNetCore --> TrackRequests
    AspNetCore --> Options
    AspNetCore --> ApiExtensions

    %% Sample Application Subcomponents
    subgraph "SampleWebApplication Components"
        direction TB
        Program["Program.cs"]:::app
        Startup["Startup.cs"]:::app
        HomeCtrl["Controllers/HomeController.cs"]:::app
        Views["Views/"]:::app
        StaticAssets["wwwroot/"]:::app
        AppSettings["appsettings*.json"]:::app
    end
    SampleApp --> Program
    SampleApp --> Startup
    SampleApp --> HomeCtrl
    SampleApp --> Views
    SampleApp --> StaticAssets
    SampleApp --> AppSettings

    %% Data Flow in Web App
    SubgraphFlow001["HTTP Request"]:::external
    SubgraphFlow002["ASP.NET Core Middleware"]:::app
    SubgraphFlow003["AspNetCoreTracker"]:::adapter
    SubgraphFlow004["TrackerBase"]:::core
    SubgraphFlow005["Google Analytics HTTP"]:::external

    SubgraphFlow001 --> SubgraphFlow002
    SubgraphFlow002 -->|invokes| Middleware
    Middleware -->|uses| TrackerSvc
    TrackerSvc -->|calls| TrackerBase
    TrackerBase -->|HTTP Post| GAEndpoint

    %% Data Flow in Console App
    ConsoleFlow001["Console Code"]:::app
    ConsoleFlow002["SimpleTracker"]:::adapter
    ConsoleFlow003["TrackerBase"]:::core

    ConsoleFlow001 -->|calls| SimpleTracker
    SimpleTracker -->|calls| TrackerBase
    TrackerBase -->|HTTP Post| GAEndpoint

    class SubgraphFlow001,SubgraphFlow002,SubgraphFlow003,SubgraphFlow004,SubgraphFlow005,ConsoleFlow001,ConsoleFlow002,ConsoleFlow003 external
```
