Install
One package for Manifest V3 extensions on Chrome, Edge, Firefox and Safari.
echo "@gotmode:registry=https://gotmode.com/sdk/npm/" >> .npmrc npm install @gotmode/metrics-extension
The .npmrc line sends every @gotmode package to Mode, so commit it with the project. pnpm, Yarn 1 and Bun read it too.
Mode serves its SDKs itself, not from a public package registry. A published version never changes, and every file and its SHA-256 is listed in SHA256SUMS.
| File | Version | SHA-256 |
|---|---|---|
gotmode-metrics-extension-0.1.0.tgz | 0.1.0 | 109724d4467c7c96d1d5b024c6602499ab96101c4f736c8930904668c183d33b |
gotmode-metrics-core-0.1.0.tgz | 0.1.0 | fac754c0bb498c57505735897073c09b2860044af54376c1a4611c095b9203c3 |
The manifest needs storage in permissions and your tracking hostname in host_permissions, for example https://m.example.com/*.
Start
The background owns the queue and the person. Call init in your service worker, or your background page on Firefox.
import mode from '@gotmode/metrics-extension';
mode.init('YOUR_INGEST_KEY', {
api_host: 'https://m.example.com',
uninstall_url: 'https://go.example.com/bye',
});In your popup, side panel and options pages, start the page helper. It reports the page opening, its taps and its errors, and sends every call to the background.
import { initPage } from '@gotmode/metrics-extension/page';
const mode = initPage();
mode.capture('saved_item');Events and people
Capture what matters in your app, and name the person once they sign in.
mode.capture('added_to_cart', { sku: 'tee-black-m', price: 24 });
mode.identify('user_42', { plan: 'pro' });
mode.group('company', 'acme', { seats: 25 });
mode.register({ app_theme: 'dark' });
mode.reset(); // on logoutA different user than the one stored starts a new person. Person properties set before identify wait for it. An empty id, one over 200 bytes, or a placeholder such as null or guest is never sent. alias, set_person_properties, unset_person_properties, register_once, register_for_session, unregister, flags (flag, flag_enabled, flag_payload) and capture_exception work as on the web.
What is captured automatically
Every event below arrives with no code beyond init and the page helper.
| Event | When |
|---|---|
$extension_installed, $extension_updated, $browser_updated | The extension is installed or updated, or the browser updates |
$extension_started | The browser starts with the extension |
$extension_page_opened, $screen | A popup, side panel or options page opens |
$click | Clicks on controls in those pages |
$exception | Errors in the background and the pages |
$tab_activated, $tab_updated | With tabs: true and the tabs permission, by host only, never the address |
Uninstalls
A browser runs no code when an extension is removed, so it opens a page instead.
With uninstall_url the browser opens that page on removal, with mode_distinct_id and mode_version added. Make it a Mode short link to a survey, and each uninstall is a click of the person who left, in the same project.
Privacy
The background takes calls only from your extension's own pages, never from a web page or another extension.
Field values are never read, and password, card and hidden fields produce nothing. Tab events carry the host alone. The browser's own user agent names the browser and the system, and the platform is extension, so your extension's numbers stay apart from your site's.
Consent and opt out
Start with consent pending, and nothing is written to the device or sent until the person answers.
mode.init('YOUR_INGEST_KEY', { api_host: 'https://m.example.com', consent: 'pending' });
mode.set_consent('granted');
mode.set_consent({ ad_storage: 'denied' });
mode.opt_out();Pending holds up to 200 events in memory. Denied drops events and deletes what was stored. Without ad storage consent, ad click ids and the Mode click id are left out. An opt out is remembered on the device and stops every send until mode.opt_in().
Delivery
Events wait on the device and are sent in batches, so nothing is lost offline and nothing is counted twice.
| Setting | Value |
|---|---|
| Batches | A second after an event, at once at 50 events, and when the app goes to the background |
| Queue | Up to 1,000 events stored on the device |
| Retries | From 1 second to 5 minutes, with the same event ids |
| Shutdown | mode.shutdown() sends what is queued and removes every listener |