Skip to content

Commit dde61a4

Browse files
authored
Separate tracker build + tests w/ Vite, Turborepo, & Playwright (#132)
* First start at turborepo * Remove temp turbo files, fix public dir * Fix gitignore * Add tracker subrepo * Move typescript to root package * Move prettier to root project * Empty tsconfig * Copy dist/tracker.js to server /public dir * Fix tests, precommit hooks * Add deploy task * Run single turbo run command on precommit * Add playwright tests for tracker script * Add test to verify request to /collect * Remove example tests * Test asserts query string is good * Remove tmp files * Attempt to fix GH actions * GH action fix attempt #2 * Add wrangler to root package.json * Update package-lock * Add "preview" turbo command, edit README w/ new turbo commands * Convert tracker to typescript * Get tracker tests into folders (get ready for multiple tests) * Rename tracker.js to reporter.js * Add missing /reporter dir * Add data-site-id initialization method * Update README for simplified initialization method * Rename workspaces to packages * Rename test file to better mirror 01_legacyInit * Add eslint to reporter package * Typecheck needs to build first * Rollback newly introduced API methods (unnecessary) * Update typescript * Same typescript version in both packages * Update package lock * Rename reporter back to tracker * Update eslint typescript tooling to suppress warnings * Update turbo dependencies
1 parent 7aa1b99 commit dde61a4

File tree

92 files changed

+25381
-7277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+25381
-7277
lines changed

.github/workflows/cd.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
with:
1515
node-version-file: "package.json"
1616
- run: npm install
17-
- run: npm run build
17+
- run: npx turbo build
1818
- uses: cloudflare/wrangler-action@v3
1919
with:
2020
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}

.github/workflows/ci.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ jobs:
1313
with:
1414
node-version-file: "package.json"
1515
- run: npm install
16-
- run: npm run lint
17-
- run: npm run build
18-
- run: npm run typecheck
19-
- run: npm run test-ci
16+
- run: npx turbo lint build typecheck test-ci --concurrency=1
2017
- name: Upload coverage reports to Codecov
2118
uses: codecov/codecov-action@v3
2219
env:

.gitignore

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
node_modules
22

3-
/.cache
4-
/.wrangler
5-
/build
6-
/public/build
3+
.cache
4+
.wrangler
5+
build
6+
dist
7+
public/build
78
.env
89

910
.dev.vars
1011

11-
coverage
12+
coverage
13+
.turbo

.husky/pre-commit

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ echo "$FILES" | xargs ./node_modules/.bin/prettier --ignore-unknown --write
1111
# Add back the modified/prettified files to staging
1212
echo "$FILES" | xargs git add
1313

14-
npm test
15-
npm run lint
16-
npm run typecheck
14+
npx turbo test lint typecheck --concurrency=1
1715

1816
exit 0

README.md

+21-31
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,42 @@ If you don't have one already, [create a Cloudflare account here](https://dash.c
2222
1. Enable [Cloudflare Analytics Engine beta](https://developers.cloudflare.com/analytics/analytics-engine/get-started/) for your account ([screenshot](https://github.com/benvinegar/counterscale/assets/4562878/ad1b5712-2344-4489-a684-685b876635d1))
2323
1. If this is your first time using Workers, you have to create a Worker before you can enable the Analytics Engine. Navigate to Workers & Pages > Overview, click the "Create Worker" button ([screenshot](./docs/create-worker.png)) to create a "Hello World" worker (it doesn't matter what you name this Worker as you can delete it later).
2424
1. Create a [Cloudflare API token](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/). This token needs `Account.Account Analytics` permissions at a minimum ([screenshot](./docs/api-token.png)).
25-
* _WARNING: Keep this window open or copy your API token somewhere safe (e.g. a password manager), because if you close this window you will not be able to access this API token again and have to start over._
25+
- _WARNING: Keep this window open or copy your API token somewhere safe (e.g. a password manager), because if you close this window you will not be able to access this API token again and have to start over._
2626

2727
### Deploy Counterscale
2828

2929
1. Download the [latest Counterscale release](https://github.com/benvinegar/counterscale/releases/latest) (or clone the repository) and extract the source files to a folder.
3030
1. With your terminal, navigate to the folder containing the source files.
3131
1. Run `npm install`
3232
1. Run `npx wrangler pages project create counterscale` and create a new Pages project.
33-
1. You will be prompted to enter the "production branch name". Just use the default provided (e.g. "main" or "production").
34-
* _NOTE: If this is your first time invoking `wrangler` on the terminal, you will be prompted to sign into your Cloudflare account._
33+
1. You will be prompted to enter the "production branch name". Just use the default provided.
34+
- _NOTE: If this is your first time invoking `wrangler` on the terminal, you will be prompted to sign into your Cloudflare account._
3535
1. Run `npx wrangler pages secret put CF_BEARER_TOKEN` → when prompted, paste the API token you created
3636
1. Run `npx wrangler pages secret put CF_ACCOUNT_ID` → when prompted, paste your Cloudflare Account ID
37-
* Find your account ID by visiting Workers and Pages > Overview. It is displayed on the right hand side of the screen.
38-
1. Run `npm run deploy` – this will do several things:
37+
- Find your account ID by visiting Workers and Pages > Overview. It is displayed on the right hand side of the screen.
38+
1. Run `npx turbo deploy` – this will do several things:
3939
1. Create a new Analytics Engine dataset, called `metricsDataset`
4040
1. Deploy the site and give you the deployment URL.
4141
1. The site should now be deployed. Visit `https://{subdomain-emitted-from-npm-run-deploy}.pages.dev`.
42-
* NOTE: _It may take take a few minutes before the subdomain becomes live._
42+
- NOTE: _It may take take a few minutes before the subdomain becomes live._
4343

44-
### Install the Tracker Script on Your Website(s)
44+
### Install the Tracking Script on Your Website(s)
4545

4646
When Counterscale is deployed, it makes `tracker.js` available at the URL you deployed to:
4747

4848
```
4949
https://{subdomain-emitted-from-npm-run-deploy}.pages.dev/tracker.js
5050
```
5151

52-
To start tracking website traffic on your web property, copy/paste the following snippet into your website HTML:
52+
To start reporting website traffic from your web property, copy/paste the following snippet into your website HTML:
5353

5454
```html
55-
<script>
56-
(function () {
57-
window.counterscale = {
58-
q: [["set", "siteId", "your-unique-site-id"], ["trackPageview"]],
59-
};
60-
})();
61-
</script>
62-
<script
63-
id="counterscale-script"
55+
<script
6456
src="https://{subdomain-emitted-from-npm-run-deploy}.pages.dev/tracker.js"
6557
defer
6658
></script>
6759
```
6860

69-
Be sure to replace `your-unique-site-id` with a unique string/slug representing your web property. Use a unique site ID for each property you place the tracking script on.
70-
7161
## Troubleshooting
7262

7363
If the website is not immediately available (e.g. "Secure Connection Failed"), it could be because Cloudflare has not yet activated your subdomain (yoursubdomain.workers.dev). This process can take a minute; you can check in on the progress by visiting the newly created worker in your Cloudflare dashboard (Workers & Pages → counterscale).
@@ -80,16 +70,16 @@ The deployment URL can always be changed to go behind a custom domain you own. [
8070

8171
### Config
8272

83-
To get started, in the project root, copy `.dev.vars.example` to `.dev.vars`.
73+
To get started, in the `packages/server` folder, copy `.dev.vars.example` to `.dev.vars`.
8474

8575
Open `.dev.vars` and enter the same values for `CF_BEARER_TOKEN` and `CF_ACCOUNT_ID` you used earlier.
8676

8777
### Running the Server
8878

8979
Counterscale is built on Remix and Cloudflare Workers. In development, you have two options:
9080

91-
1. `npm run dev` → This runs the Vite development server in Node.js. This server will automatically rebuild files when you change them, but it does not best reflect Cloudflare's serverless platform.
92-
2. `npm run preview` → This runs Cloudflare's Miniflare server with a build of the Remix files. This closer matches the deployment environment, but does not (yet) automatically rebuild your app.
81+
1. `npx turbo dev` → This runs the Vite development server in Node.js. This server will automatically rebuild files when you change them, but it does not best reflect Cloudflare's serverless platform.
82+
2. `npx turbo preview` → This runs Cloudflare's Miniflare server with a build of the Remix files. This closer matches the deployment environment, but does not (yet) automatically rebuild your app.
9383

9484
## Notes
9585

@@ -99,8 +89,8 @@ There is only one "database": the Cloudflare Analytics Engine dataset, which is
9989

10090
Right now there is no local "test" database. This means in local development:
10191

102-
- Writes will no-op (no hits will be recorded)
103-
- Reads will be read from the production Analaytics Engine dataset (local development shows production data)
92+
- Writes will no-op (no hits will be recorded)
93+
- Reads will be read from the production Analaytics Engine dataset (local development shows production data)
10494

10595
### Sampling
10696

@@ -116,10 +106,10 @@ The primary goal of Counterscale is to be super easy to self-host and maintain.
116106

117107
To achieve that:
118108

119-
- There should be no application state outside of CF Analytics Engine
120-
- e.g. no additional relational database like MySQL, PostgreSQL, etc.
121-
- That means no `users` table, no `sites` table, etc.
122-
- This also means retention will be limited by what CF Analytics Engine provides. While it could be possible to stand up a "hit counter" for long-lived data (e.g. years), that would mean another database, which we will not pursue.
123-
- We prioritize backwards compatibility
124-
- New `metricsDataset` columns can be added, but old columns cannot be removed or renamed (they can however, be "forgotten").
125-
- That also means it's okay if a feature only works during a period where the data is active.
109+
- There should be no application state outside of CF Analytics Engine
110+
- e.g. no additional relational database like MySQL, PostgreSQL, etc.
111+
- That means no `users` table, no `sites` table, etc.
112+
- This also means retention will be limited by what CF Analytics Engine provides. While it could be possible to stand up a "hit counter" for long-lived data (e.g. years), that would mean another database, which we will not pursue.
113+
- We prioritize backwards compatibility
114+
- New `metricsDataset` columns can be added, but old columns cannot be removed or renamed (they can however, be "forgotten").
115+
- That also means it's okay if a feature only works during a period where the data is active.

0 commit comments

Comments
 (0)