Skip to content

npm / Astro

The @moszaic/tracker-astro package runs as server-side middleware — it captures every request before your app code runs, including all bot traffic regardless of JavaScript execution.

The package is distributed via a private registry. Your MOSZAIC write key is the only credential you need — no AWS access required.

Exchange your write key for a short-lived registry token. Replace wk_your_write_key_here with your write key from Settings → Properties:

Terminal window
curl -s \
-H "X-Moszaic-Write-Key: wk_your_write_key_here" \
https://api.moszaic.ai/npm/token

Response:

{
"token": "...",
"registry_url": "https://moszaic-714412036813.d.codeartifact.us-east-2.amazonaws.com/npm/moszaic-npm/",
"expires_in": 43200
}

The token is valid for 12 hours and is only needed at install time — it never touches your live site.

Use the token to install the package from the private registry:

Terminal window
TOKEN=$(curl -s \
-H "X-Moszaic-Write-Key: wk_your_write_key_here" \
https://api.moszaic.ai/npm/token | jq -r '.token') && \
NPM_CONFIG_@moszaic:registry=https://moszaic-714412036813.d.codeartifact.us-east-2.amazonaws.com/npm/moszaic-npm/ \
NPM_CONFIG_//moszaic-714412036813.d.codeartifact.us-east-2.amazonaws.com/npm/moszaic-npm/:_authToken=$TOKEN \
npm install @moszaic/tracker-astro

Create src/middleware.ts in your Astro project. Your Settings → Instrumentation → npm tab has this pre-filled with your credentials:

import { createMoszaicMiddleware } from '@moszaic/tracker-astro';
export const onRequest = createMoszaicMiddleware({
propertyId: 'your-property-id',
writeKey: 'wk_your_write_key_here',
apiUrl: 'https://tracker.moszaic.ai',
});

Store your write key as MOSZAIC_WRITE_KEY in your repository secrets, then add these steps to your pipeline:

- name: Set up MOSZAIC registry
run: |
TOKEN=$(curl -s \
-H "X-Moszaic-Write-Key: ${{ secrets.MOSZAIC_WRITE_KEY }}" \
https://api.moszaic.ai/npm/token | jq -r '.token')
npm config set @moszaic:registry https://moszaic-714412036813.d.codeartifact.us-east-2.amazonaws.com/npm/moszaic-npm/
npm config set //moszaic-714412036813.d.codeartifact.us-east-2.amazonaws.com/npm/moszaic-npm/:_authToken "$TOKEN"
- name: Install dependencies
run: npm ci

After deploying, visit your site and check the MOSZAIC dashboard. Bot and human events should appear within seconds.