{"id":13483389,"url":"https://github.com/SukkaW/cloudflare-workers-async-google-analytics","last_synced_at":"2025-03-27T14:31:18.187Z","repository":{"id":35049258,"uuid":"200640872","full_name":"SukkaW/cloudflare-workers-async-google-analytics","owner":"SukkaW","description":":cloud: The Cloudflare Workers implementation of an async Google Analytics","archived":false,"fork":false,"pushed_at":"2022-01-26T10:08:46.000Z","size":19,"stargazers_count":222,"open_issues_count":5,"forks_count":32,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-19T22:18:44.205Z","etag":null,"topics":["cloudflare","cloudflare-workers","google-analytics"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SukkaW.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-05T11:16:08.000Z","updated_at":"2025-03-10T07:40:45.000Z","dependencies_parsed_at":"2022-07-19T05:03:56.271Z","dependency_job_id":null,"html_url":"https://github.com/SukkaW/cloudflare-workers-async-google-analytics","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2Fcloudflare-workers-async-google-analytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2Fcloudflare-workers-async-google-analytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2Fcloudflare-workers-async-google-analytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2Fcloudflare-workers-async-google-analytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SukkaW","download_url":"https://codeload.github.com/SukkaW/cloudflare-workers-async-google-analytics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245276191,"owners_count":20588894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare","cloudflare-workers","google-analytics"],"created_at":"2024-07-31T17:01:10.703Z","updated_at":"2025-03-27T14:31:17.926Z","avatar_url":"https://github.com/SukkaW.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# cloudflare-workers-async-google-analytics\n\n[![Author Sukka](https://img.shields.io/badge/author-Sukka-b68469.svg?style=flat-square)](https://skk.moe)\n[![License MIT](https://img.shields.io/github/license/sukkaw/cloudflare-workers-async-google-analytics.svg?style=flat-square)](./LICENSE)\n![NPM Version](https://img.shields.io/npm/v/cfga?style=flat-square)\n[![Build with Cloudflare Workers](https://img.shields.io/badge/build%20with-cloudflare%20workers-f38020.svg?style=flat-square)](https://workers.cloudflare.com/)\n![Gzip size of cfga.min.js](https://img.badgesize.io/sukkaw/cloudflare-workers-async-google-analytics/master/cfga.min.js.svg?compression=gzip\u0026style=flat-square)\n[![](https://data.jsdelivr.com/v1/package/npm/cfga/badge)](https://www.jsdelivr.com/package/npm/cfga)\n\n\nThe Cloudflare Workers implementation of an async Google Analytics\n\n## Introduction\n\nThis project is based on [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/), using [Cloudflare Workers](https://workers.cloudflare.com/) with a less than 1KB gzipped tiny `cfga.min.js` to accelerate the Google Analytics, rather than a heavy (45KB gzipped) `analytics.js` from Google.\n\n## Get Start\n\n### 1. Import into Cloudflare Workers\n\nLogin into Cloudflare Dashboard and enter `Workers` App. Create a new script, delete default code in the editor, and then copy [the `woker.js` content](https://github.com/SukkaW/cloudflare-workers-async-google-analytics/blob/master/worker.js) into the editor. After saving the workers script, do not forget to register a route for the scripts.\n\nNow you can test your workers with a simple HTTP request. You should able to see `403 Forbidden`. Then you can deploy the scripts.\n\n### 2. Insert the `cfga.min.js` into your website\n\nJust add those few lines of the code to your website, right before `\u003c/body\u003e`. Do not forget to replace the default configuration with your own!\n\n```html\n\u003cscript\u003e\nwindow.ga_tid = \"UA-XXXXX-Y\"; // {String} The trackerID of your site.\nwindow.ga_api = \"https://example.com/xxx/\"; // {String} The route of your cloudflare workers you just registered before.\n\u003c/script\u003e\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/cfga@1.0.3\" async\u003e\u003c/script\u003e\n```\n\n### 3. Watch this repo with `Releases Only`.\n\nClick the `watch` button at the top of the repo and choose `Releases Only`, so you can get notice of release update in time.\n\n## Notice\n\n### For SPA site\n\n`cfga.js` will send the data once the window's `load` event is fired.\n\nBut you can still manually send the data again by calling `window.cfga()`, just like the original `analytics.js` with `ga('send', 'pageview')`.\n\n### Say hello to EasyList\n\nRecently `cloudflare-workers-async-google-analytics` has been blocked by [EasyList](https://github.com/easylist/easylist/commit/2ce2444193f5d479fb131e9dc89bbde9c82c8ad1). Great Job though. So I am going to play a cat \u0026 mouse game now.\n\nFrom `1.0.3` the random string will be added as a parameter to bypass EasyList. Also, this could help, too:\n\n- Add the route `cfga/jquery.js` for your Cloudflare Workers.\n- Use setup as below:\n\n```js\nwindow.ga_api = \"https://example.com/cfga/jquery.js\"; // {String} The route of your cloudflare workers you just registered before.\n```\n\nBlock `jquery.js` if you can, haha!\n\n## Advanced\n\n### Data type the `cfga.min.js` collected and sent\n\nCurrently, `cloudflare-workers-async-google-analytics` and `cfga.min.js` only support collect those types of data listed below. If you want to collect more, you should use Google Analytics official track code.\n\n- [`dl`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dl): Document location URL\n- [`uip`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uip): User real IP\n- [`ua`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ua): User Agent\n- [`dt`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dt): Document Title\n- [`de`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#de): Document Encoding\n- [`dr`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dr): Document Referrer\n- [`ul`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ul): User Language\n- [`sd`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sd): Screen Colors Depth\n- [`sr`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sr): Screen Resolution\n- [`plt`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#plt): Page Load Time\n- [`dns`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dns): DNS Time\n- [`pdt`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#pdt): Page Downloaad Time\n- [`rrt`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#rrt): Redirect Response Time\n- [`tcp`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tcp): TCP Connect Time\n- [`srt`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#srt): Server Response Time\n- [`dit`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dit): DOM Interactive Time\n- [`clt`](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#clt): Content Load Time\n\n### Security\n\n`cloudflare-workers-async-google-analytics` blocks those types of request by default:\n\n- No `User-Agent` in request headers\n- No `Referer` in request headers\n- No Tracker ID given in request headers\n- Use some other measurements from Cloudflare WAF\n\nAnd if you want to restrict your workers only for your website, all you need to do is to edit a few lines of your workers:\n\n- Launch Cloudflare Workers Editor again.\n- You can see some commented out code at the first line like this:\n\n```javascript\n//const AllowedReferrer = 'skk.moe';\n```\n\n- replace your domain with `skk.moe`, then remove `//`.\n\n\u003e **Notice**: set `AllowedReferrer` value to `skk.moe` means all the subdomains of `skk.moe` will be allowed as well.\n\n## Author\n\n**cloudflare-workers-async-google-analytics** © [Sukka](https://github.com/SukkaW), Released under the [MIT](./LICENSE) License.\u003cbr\u003e\nAuthored and maintained by Sukka with help from contributors ([list](https://github.com/SukkaW/cloudflare-workers-async-google-analytics/graphs/contributors)).\n\n\u003e [Personal Website](https://skk.moe) · [Blog](https://blog.skk.moe) · GitHub [@SukkaW](https://github.com/SukkaW) · Telegram Channel [@SukkaChannel](https://t.me/SukkaChannel) · Twitter [@isukkaw](https://twitter.com/isukkaw) · Keybase [@sukka](https://keybase.io/sukka)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSukkaW%2Fcloudflare-workers-async-google-analytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSukkaW%2Fcloudflare-workers-async-google-analytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSukkaW%2Fcloudflare-workers-async-google-analytics/lists"}