Coder Social home page Coder Social logo

jwstanly / simple-analytics Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 50 KB

A Cloudflare Worker to track analytics for Google Analytics and/or Cloudflare KV

TypeScript 100.00%
analytics cloudflare-kv cloudflare-workers google-analytics ga4 cloudflare

simple-analytics's Introduction

Simple Analytics

A Cloudflare Worker to track online analytics. The Worker's URL serves an invisible 1x1 PNG, which tracks clients that load the image on web, email, mobile, etc.

Tracks:

  • Location
  • Time
  • Cloudflare origin IP address
  • A "source" parameter declared through the URL param

Integrates with:

  1. Google Analytics - Forward events to GA4
  2. Cloudflare KV - Persist events in a KV namespace

Setup:

  1. Install Cloudflare's Wrangler CLI v2.0.0+ (more details). Run wrangler config to connect your Cloudflare account.
  2. Rename wrangler.sample.toml file to wrangler.toml
  3. [Optional] For Google Analytics, in wrangler.toml add your GA4 measurement id and API secret key (more details).
  4. [Optional] For Cloudflare KV, create a KV namespace (more details). Then in wrangler.toml add your KV namespace id to bind your Worker to this namespace (more details).
  5. Tweak the rest of wrangler.toml config as needed. You can change the Cloudflare project's name (appears in the public URL's subdomain), plus environment variables SOURCE_QUERY_PARAM (the URL query param name to specify source), PNG_BASE64 (the base64 encoded PNG to serve, by default a 1x1 transparent PNG), and GA4_EVENT_NAME (the event name for Google Analytics).
  6. Run wrangler deploy in your terminal. Your Worker will now be live, any requests will be tracked.

Usage

Your Worker's URL follows this pattern. Optionally add the query parameter to track custom sources:

https://<PROJECT-NAME>.<CLOUDFLARE-USERNAME>.workers.dev/?<SOURCE_QUERY_PARAM>=<CUSTOM_SOURCE_HERE>

Use the URL as an image source on any platform, like HTML:

<img src="https://my-project.jwstanly.workers.dev/?s=custom-source-tracking-here">

For Google Analytics on the dashboard go to Reports -> Engagement -> Events. For KV on the Cloudflare dashboard go to Workers & Pages -> KV -> Tracking (View).

simple-analytics's People

Contributors

jwstanly avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

codebeat

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.