Docs
Install the app
The app is two canisters: the backend and the dashboard. The engine console shows them as one app, "Analytics". You need git, Node.js (with npm), the icp CLI, mops and ic-wasm. The install script checks that they are installed. This line installs the last three:
npm install --global @icp-sdk/icp-cli @icp-sdk/ic-wasm ic-mopsInstall the app on your engine
Only an engine admin can create canisters. The subnet ID is on the Settings page of the engine.
git clone https://github.com/yhabib/cloud-engine-analytics
cd cloud-engine-analytics
git checkout "$(git tag --list 'v*' --sort=-v:refname | head -n 1)"
icp identity link web <name> --app <engine-console-host>
icp identity default <name>
bash scripts/install.sh <subnet-id>The checkout line picks the newest release, so you deploy a tested release and not the head of main. The script checks the tools, deploys both canisters to that subnet and prints the dashboard URL and the next step. If the app is already installed, it stops and points to the upgrade command. The end of its output looks like this:
Installed Cloud Engine Analytics.
Dashboard: https://<app-id>.icp.net
Backend: https://<analytics-id>.icp.net
Next step: open the dashboard, sign in with Internet Identity, and run this once with the principal it shows:
icp canister call -e ic analytics add_owner '(principal "<your-principal>")'
Add your site
Open the dashboard at the URL the script printed. The "Open" button of the app in the engine console works too. Sign in with Internet Identity. Add a site with its allowed origins.
A new install has no owners. If the dashboard says "not an owner", it shows your principal. Run this command once from the repo folder, with the identity that deployed. It works because that identity is a controller.
icp canister call -e ic analytics add_owner '(principal "<your-principal>")'Then click "Check again".
Add the script tag
Add this tag to every page:
<script defer
src="https://<your-dashboard-id>.icp.net/tracker.js"
data-site-id="<your-site-id>"
data-canister-url="https://<your-backend-id>.icp.net">
</script>Upgrade
Run these commands from the repo folder: fetch the tags, check out the newest release and deploy. It keeps the data. See the releases page for what changed.
git fetch --tags
git checkout "$(git tag --list 'v*' --sort=-v:refname | head -n 1)"
icp deploy -e icNever use reinstall. It deletes all data.
Install with the script tag
Add this tag to every page:
<script defer
src="https://<your-dashboard-id>.icp.net/tracker.js"
data-site-id="<your-site-id>"
data-canister-url="https://<your-backend-id>.icp.net">
</script>
data-site-id is the site ID you added with add_site. data-canister-url is the URL of your backend canister. The script posts to that URL plus /e?site= followed by the site ID.
It sends a page view on load and on each route change to a new path (pushState, replaceState, back and forward). It does nothing on localhost.
A hash change to a route hash (#/...) also sends a page view. Its path is the pathname joined with the route, without the # and without any query or second #: /#/about is recorded as /about. A plain anchor like #section sends nothing. A hash route /#/about and a real page /about are recorded as the same path. To turn this off, add data-hash-routes="false" to the tag.
To group paths, add data-path-patterns with one or more patterns separated by spaces or commas, for example data-path-patterns="/proposal/:id /user/:principal". A segment that starts with : matches any one segment, so /proposal/123 and /proposal/456 are both sent as /proposal/:id. The first matching pattern wins, and a trailing slash on the path is ignored. The grouped path is what the script sends, so the ID never leaves the browser. A pattern starts with /, has no trailing slash, and has at least one :name segment; invalid patterns are ignored.
To send a custom event:
cea.track("signup");
A custom event may also have one optional value: cea.track("signup", "newsletter").
Recipes for dapps
Call this after a wallet or Internet Identity login succeeds:
cea.track("wallet_connect", "plug");
Call this after an awaited canister call returns without error, with the method name as the value:
cea.track("canister_call_ok", "transfer");
The package takes the same patterns as a list, in the pathPatterns: ["/proposal/:id"] option of init.
The same works with track from the package. To turn off hash route page views in the package, call init with hashRoutes: false.
The optional second argument is one value. It is stored as name:value (for example wallet_connect:plug) and counted like any custom event. Each part is cut to 64 characters. The limit of 1000 distinct event keys per day is the same, so values must come from a small fixed set, such as a wallet kind (ii, plug, oisy) or a method name.
Never send a principal, account ID, wallet address or any other user identifier as the name or the value.
Content Security Policy
If your app sends a Content Security Policy, it needs two entries.
script-src needs the origin of your dashboard, https://<your-dashboard-id>.icp.net. It serves tracker.js.
connect-src needs your backend canister URL, https://<your-backend-id>.icp.net. Beacons are POSTs to <canister-url>/e?site=<site-id>.
For an asset canister, set the header in .ic-assets.json5:
[
{
"match": "**/*",
"headers": {
// Keep your app's other directives and merge these two in.
"Content-Security-Policy": "default-src 'self'; script-src 'self' https://<your-dashboard-id>.icp.net; connect-src 'self' https://<your-backend-id>.icp.net"
}
}
]
Allowed origins and custom domains
The canister accepts a beacon only when the browser's Origin header is one of the origins of that site. Otherwise it answers 403.
An owner sets the origins with add_site(id, origins). Calling it again with the same ID replaces the whole list, so pass every origin each time.
An owner can give a principal read-only access to one site with add_viewer(site, principal) or in the Viewers section of /settings. A viewer can read the stats of that site and change nothing.
An origin is scheme://host[:port]. It is stored in lowercase without a trailing slash. It has no path, query or fragment.
Each domain your app is served from is its own origin. List the canister URL of the app (https://<app-canister-id>.icp.net), each custom domain (https://example.com), and www or other subdomains (https://www.example.com).
add_site("my-app", vec { "https://<app-canister-id>.icp.net"; "https://example.com"; "https://www.example.com" })
data-canister-url stays the backend canister URL on every domain.
A site ID is 1 to 64 characters of a-z, 0-9 and -.
What the canister stores
The canister stores daily aggregates per site, per UTC day: page views, sessions and unique visitors. It keeps them in total and by path, referrer host, UTM source, device class, browser, OS, language and time zone. Custom events keep a count and unique visitors per name (or name:value when a value is sent) and UTC day, counting visitors who sent a page view to that site the same day; older events keep their count with 0 visitors, and the month of the upgrade has partial event visitors.
It keeps 13 months (396 days) and drops older days. delete_site_data(site) deletes every stored day of a site. Only an owner can call it.
It never stores:
- IP addresses.
- Single hits or a log of requests.
- Visitor hashes of past days. The visitor hash is SHA-256 of the IP, User-Agent, site ID and a salt that changes each UTC day, and only the current day's salt is kept. A timer at 00:00 UTC drops the salt, the visitor hashes and the session state of the day before, even when no beacon arrives, and it runs again after each upgrade.
- The tracker uses no cookies, localStorage or sessionStorage.
The country is approximate for now and comes from the browser time zone.
Limits and abuse
The canister stops these beacons:
- A site ID that does not exist, or an
Originthat is not allowed for the site: 403. - No
X-Real-IP, or an empty one: 400. - A body that does not parse, or is over 2048 bytes: 400.
- More than 30 beacons per minute from one IP (only when the IP is real): 429.
- More than 3000 beacons per minute for one site: 429.
It stores no IPs and no single hits, so forged beacons cannot leak data of real visitors.
The canister cannot stop these:
Originis only checked against the allowed list. A script outside a browser can set anyOrigin.http_request_updateis a public method. Anyone can call it directly, without the gateway, with any headers, includingX-Real-IP.- Through the gateway, the source of
ic-gatewayreplaces a client-sentX-Real-IPwith the IP it sees, unless the sender is a trusted network. This is read from source only. It is not confirmed on the live gateway, so do not rely on it.
So one script with random X-Real-IP values can use the whole 3000-per-minute budget of a site. Real visitors of that site then get 429, and their page views are not counted for that minute. Forged beacons also count as page views and visitors, and can fill the daily caps: 100000 visitors per site per day, 1000 keys per list per day (more count as "(other)"), and 20000 live visitors per site.
What you can do:
- Treat the counts as only as good as the beacons. A spike of new visitors from one site can be forged.
- An owner can delete the stored data of a site with
delete_site_data(site). It deletes every stored day of the site.
There is no filter for forged beacons yet.