How do I get the SDK for an HTML5 game?

The SDK is not on npm yet, but its source is public on GitHub under the MIT license (manalens/manalens-js). Install it from GitHub with npm, or build the browser bundle (snippet.global.js) with pnpm build in that repository and host the file yourself.

  1. Install the SDK from GitHub, or build the browser bundle from the repository.
  2. Add the script tag to the game page, or import the package in a bundled game.
  3. Call track() when the game state changes, never in the requestAnimationFrame loop.
  4. On a game portal other than itch.io, add the host of the game frame to Allowed origins.
  5. Open the game and check the project activity feed.

How do I add the script tag to the game page?

Copy snippet.global.js next to your game files. Add the script once in the page that loads the game. Replace both placeholders; the Publishable key is public and scoped to your project. The script exposes window.manalens after loading.

HTML in the game page

<script src="/snippet.global.js"
  data-publishable-key="YOUR_PUBLISHABLE_KEY"
  data-endpoint="https://api.your-domain.com"
  defer></script>
<script>
  window.manalens?.track('level_complete', { level: 3 });
</script>

How do I import the SDK in a bundled game?

For a game built with Vite, webpack or another bundler, install the SDK and create one instance for the game. The app option adds the platform, version and build to every event, so you can read funnels and errors by build.

src/analytics.js

// npm install github:manalens/manalens-js
import { createAnalytics } from '@manalens/web';

export const analytics = createAnalytics({
  publishableKey: 'YOUR_PUBLISHABLE_KEY',
  endpoint: 'https://api.your-domain.com',
  app: { platform: 'web', engine: 'other', app_version: '1.0.0', build: '1' },
});

When should a canvas game send events?

Call track() when the game state changes: a level starts, a level ends, a purchase opens. Never call it in the requestAnimationFrame loop, because the loop runs on every frame. When the tab closes, send the last events with flush(true), which uses fetch with keepalive. The same code works in Phaser and PixiJS. A Three.js game has its own guide, with asset loading and lost WebGL contexts.

Canvas game code

import { analytics } from './analytics';

function endLevel(level, score, won) {
  analytics.track(won ? 'level_complete' : 'level_fail', { level, score });
}

function frame() {
  update();
  draw(context); // no track() here
  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);

window.addEventListener('pagehide', () => analytics.flush(true));

Does it work on game portals and my own domain?

Serve the game over HTTPS, because the SDK makes IDs with crypto.randomUUID(). On your own domain, register the site origin; the game and the website then count one player. On itch.io, *.itch.zone is in Allowed origins by default. For another portal, add the host that serves the game frame in Project settings, Allowed origins.

Page views are automatic by default. In a single-page game with its own screens, set autoPageviews: false and send track() events for screens instead.

How do I check that HTML5 game events arrive?

Register your actual site origin and use its Publishable key. The endpoint is your deployed API origin, not a Tinybird URL. Open the published site, check that the bundle loads, and inspect successful collector requests and the project activity feed. Collection is queued, so allow for delivery latency.

Trigger a second page and a custom event. Confirm one expected pageview per navigation. Disable duplicate analytics installations. If your game has accounts, identify a player only after login and clear the SDK user ID at logout. Review campaign fields and properties on a non-sensitive page first.

Frequently asked questions

Is there a Phaser or PixiJS plugin?

No. The Manalens JavaScript SDK is plain browser code with no framework link. Create one instance and call track() from your game code when the game state changes. Do not call it in the animation loop. The same code works in Phaser, PixiJS, and Three.js, which has its own guide.

Is the npm package public?

No. The SDK is not on npm yet, but the source is public on GitHub under the MIT license (manalens/manalens-js). Install it with npm install github:manalens/manalens-js, or build the browser bundle with pnpm build in that repository and host the snippet.global.js file yourself.

Which endpoint do I use?

Use your deployed analytics API origin, such as https://api.your-domain.com. Do not use a Tinybird URL. Register your actual site origin and use the Publishable key for that site. The Publishable key is public and safe in browser code. Never put Tinybird or database credentials in browser code.

How do I check installation?

Open the published site and check that the bundle loads. Inspect the requests to Manalens and the project activity feed. Then trigger a second page and a custom event, and confirm one pageview per navigation. Collection is queued, so allow for some delivery delay. Disable any duplicate analytics installations.

Do I need to send pageviews myself?

Usually not. Page views are automatic by default. The SDK sends the first page, pushState and replaceState path changes, and back and forward navigation. For a router with its own page-load event, set autoPageviews to false and call page from that event. You can also dispatch manalens:navigate after the new page shows.

Sources and implementation