Giter Club home page Giter Club logo

google-analytics-proxy's Introduction

Google Analytics Proxy

License Go Reference Go Report Card Actions Packages Releases

Google Analytics Proxy

๐Ÿ“ˆ Transparent HTTP proxy for tracking pageviews with Google Analytics

Motivations

There are a number of situations where it is difficult (or impossible) to utilize the traditional Google Analytics tracking scripts.

  • Server-side problems
    • What if the page is dynamically generated, or the contents isn't under your direct control?
    • What if the page return an HTTP redirect?
    • What if the page returns non-HTML content?
  • Client-side problems:
    • What if the browser blocks Google Analytics?
    • What if the browser has JavaScript disabled?
    • What if the browser doesn't even use JavaScript (like curl)?

This application is an option to solve all of these problems.

What this application is not

  • A proxy for https://www.google-analytics.com/collect.
  • A proxy for serving analytics.js.
  • A JavaScript library.

How it works

This application is an HTTP proxy server; It listens for a client HTTP request, forwards the request to an upstream HTTP server, waits for a response from the upstream server, and finally returns that response back to the original client.

From the client's perspective everything is functioning identically to if they were connecting to the upstream directly, but all of their traffic is actually being transparently proxied.

While this is happening, each request and response is used to construct a pageview event, that is then reported to Google Analytics.

Since there is no JavaScript whatsoever, it is not possible to disable Google Analytics reporting. ๐Ÿšซ

Additionally, the upstream HTTP service doesn't need to integrate with (or have any knowledge of) Google Analytics.

Installing

A release version Docker image can be pulled by running:

docker pull ghcr.io/joshdk/google-analytics-proxy:v0.1.0

Or, a development version binary can be installed by running:

go install github.com/joshdk/google-analytics-proxy@master

Configuration

This tool uses several environment variables as configuration.

Name Purpose Example
$LISTEN Host and port that the proxy will listen on. 0.0.0.0:8080
$TLS_CERT_PATH (Optional) Path to TLS certificate file. /path/to/tls.pem
$TLS_KEY_PATH (Optional) Path to TLS private key file. /path/to/tls.key
$UPSTREAM_ENDPOINT Address of the upstream service to be proxied. https://example.com
$UPSTREAM_HOSTNAME (Optional) Hostname to used when proxying requests to the upstream. example.com
$GOOGLE_ANALYTICS_TRACKING_ID Tracking ID for your Google Analytics property UA-123456789-1
$GOOGLE_ANALYTICS_PROPERTY_NAME Name of your Google Analytics property. example.com
$GOOGLE_ANALYTICS_DRY_RUN (Optional) Disables Google Analytics reporting. true

Usage

To run the Docker image, you can use a command like:

docker run \
  --rm \
  -p 8080:8080 \
  -e LISTEN=0.0.0.0:8080 \
  -e UPSTREAM_ENDPOINT=https://example.com \
  -e UPSTREAM_HOSTNAME=example.com \
  -e GOOGLE_ANALYTICS_PROPERTY_NAME=example.com \
  -e GOOGLE_ANALYTICS_TRACKING_ID=UA-123456789-1 \
    ghcr.io/joshdk/google-analytics-proxy:v0.1.0

Or, to run the local binary, you can use a command like:

LISTEN=0.0.0.0:8080 \
UPSTREAM_ENDPOINT=https://example.com \
UPSTREAM_HOSTNAME=example.com \
GOOGLE_ANALYTICS_PROPERTY_NAME=example.com \
GOOGLE_ANALYTICS_TRACKING_ID=UA-123456789-1 \
  $GOPATH/bin/google-analytics-proxy

Kubernetes

This application is designed to be deployed into a Kubernetes cluster, ideally as a side-car container inside the same pod as your existing web service. While not the only configuration option, this technique is beneficial because it enables you to:

  • Proxy your service with minimal networking overhead.
  • Scale along with your service.
  • Be configured in the same deployment spec as your service.

As a demo, there is a kubernetes directory, containing a kustomize manifest which can be deployed with kubectl apply -k ./kubernetes. Afterwards, you must run kubectl port-forward svc/demo 8080:8080 in order to expose the service locally.


In all cases, browsing to https://localhost:8080 afterwards will display the proxied upstream. Realtime pageviews should also appear in your Google Analytics dashboard.

License

This code is distributed under the MIT License, see LICENSE.txt for more information.

google-analytics-proxy's People

Contributors

joshdk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

google-analytics-proxy's Issues

Why is the proxy limited to only 1 property/trackingID? And to version 1?

Hi @joshdk ,

I'm wondering why it's not possible to leave the choice of the destination property to the frontend? Is there any limitation for that? Same for the version hardcoded to "1"?

The proxy could totally rely on the request from the user no? For example by receiving ?v=2&tid=G-AAAAAAA&gtm=2o we can forward to the version "2" with the right trackingID. And for more security the docker could take as environment variable a whitelist of multiple trackingIDs?

What do you think?

(I did not try for now, just looking around which proxy would fit forwarding to GA4 with geolocation...)

Thank you,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.