posthog-js 1.439.2 → 1.440.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -0
- package/dist/all-external-dependencies.js +1 -1
- package/dist/array.full.es5.js +1 -1
- package/dist/array.full.js +1 -1
- package/dist/array.full.no-external.js +1 -1
- package/dist/array.js +1 -1
- package/dist/array.no-external.js +1 -1
- package/dist/customizations.full.js +1 -1
- package/dist/customizations.js +1 -1
- package/dist/default-extensions.js +1 -1
- package/dist/extension-bundles.js +1 -1
- package/dist/lazy-recorder.js +1 -1
- package/dist/loader.d.ts +1 -0
- package/dist/loader.js +2 -0
- package/dist/loader.js.map +1 -0
- package/dist/main.js +1 -1
- package/dist/module.full.cjs +1 -1
- package/dist/module.full.js +1 -1
- package/dist/module.full.no-external.cjs +1 -1
- package/dist/module.full.no-external.js +1 -1
- package/dist/module.js +1 -1
- package/dist/module.mjs +1 -1
- package/dist/module.no-external.cjs +1 -1
- package/dist/module.no-external.js +1 -1
- package/dist/module.slim.js +1 -1
- package/dist/module.slim.no-external.js +1 -1
- package/dist/posthog-recorder.js +1 -1
- package/dist/src/entrypoints/loader.d.ts +1 -0
- package/dist/surveys-preview.js +1 -1
- package/dist/surveys.js +1 -1
- package/lib/package.json +1 -1
- package/lib/src/entrypoints/loader.d.ts +1 -0
- package/lib/src/entrypoints/loader.js +194 -0
- package/lib/src/entrypoints/loader.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -11,6 +11,58 @@ SDK usage examples and code snippets live in the official documentation so they
|
|
|
11
11
|
|
|
12
12
|
- [JavaScript library docs](https://posthog.com/docs/libraries/js)
|
|
13
13
|
|
|
14
|
+
## Single script tag
|
|
15
|
+
|
|
16
|
+
```html
|
|
17
|
+
<script
|
|
18
|
+
src="https://us-assets.i.posthog.com/static/loader.js?project_token=phc_YOUR_PROJECT_TOKEN"
|
|
19
|
+
crossorigin="anonymous"
|
|
20
|
+
></script>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`crossorigin="anonymous"` is recommended but optional on the loader tag. Without it, the loader still works, but uses the browser's default script request mode and has limited cross-origin error reporting. The loader cannot change the mode of its own request after download. The full SDK script it injects always uses `crossOrigin = 'anonymous'`, regardless of the outer tag.
|
|
24
|
+
|
|
25
|
+
Use `eu-assets.i.posthog.com` for EU projects. Every project loads the same static file: the loader reads `project_token` from its own URL, falling back to `data-project-token`. Putting the token in the URL path instead would require additional routing, as with Sentry's loader.
|
|
26
|
+
|
|
27
|
+
The small loader exposes queued public SDK methods, including `people.set` and `people.set_once`, and asynchronously loads an immutable SDK bundle without forwarding the query string. Its method list is generated from the SDK class during the build, so it stays in sync without bundling the SDK itself. Existing snippet and npm installations are unchanged; use only one installation method per page.
|
|
28
|
+
|
|
29
|
+
Include the tag before application scripts that call `posthog`. Do not add `async` or `defer` if those scripts need the queue immediately. Calls before the full SDK loads are queued; synchronous getters cannot return SDK values yet. The loader does not buffer browser interactions or errors that happen before the SDK installs its listeners.
|
|
30
|
+
|
|
31
|
+
| Attribute | Purpose |
|
|
32
|
+
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
33
|
+
| `data-project-token` | Project token fallback when the URL has no `project_token` value. |
|
|
34
|
+
| `data-api-host` | Ingestion host, including relative proxy paths such as `/ingest`. Inferred for the standard US/EU asset hosts; required for custom asset hosts. |
|
|
35
|
+
| `data-defaults` | Defaults snapshot override. If neither this attribute nor `data-config.defaults` is set, the SDK uses `unset`. |
|
|
36
|
+
| `data-config` | JSON object with SDK options, applied before initialization. Explicit `data-api-host` and `data-defaults` attributes take precedence. |
|
|
37
|
+
| `data-defer-init` | Set to `true` to wait for an explicit `init()` call. Defaults to `false`. |
|
|
38
|
+
|
|
39
|
+
For consent-gated automatic installation, set `data-config='{"opt_out_capturing_by_default":true}'` on the tag before it executes.
|
|
40
|
+
|
|
41
|
+
### Deferred initialization
|
|
42
|
+
|
|
43
|
+
Set `data-defer-init="true"` to download the SDK without automatically initializing it. The default is `false`. Pass the project token and all configuration to an explicit `posthog.init()` call instead. The automatic-init token and configuration attributes are not used in this mode.
|
|
44
|
+
|
|
45
|
+
```html
|
|
46
|
+
<script src="https://us-assets.i.posthog.com/static/loader.js" data-defer-init="true" crossorigin="anonymous"></script>
|
|
47
|
+
<script>
|
|
48
|
+
posthog.init('phc_TOKEN', {
|
|
49
|
+
api_host: 'https://us.i.posthog.com',
|
|
50
|
+
defaults: '2026-08-30',
|
|
51
|
+
})
|
|
52
|
+
posthog.capture('example event')
|
|
53
|
+
</script>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Call `init()` before other SDK methods. If the SDK is still downloading, initialization and subsequent calls are queued. If it has already downloaded, `init()` uses the normal SDK API. Deferred initialization supports function-valued configuration, such as `before_send`, and named instances. It does not defer downloading the scripts and is separate from HTML's `defer` attribute.
|
|
57
|
+
|
|
58
|
+
The mutable `/static/loader.js` and `/static/<major>/loader.js` aliases embed their SDK build version and load `/static/<build-version>/array.js`. A cached loader therefore keeps using its matching SDK even when the aliases change. The loader does not fall back to the mutable `/static/array.js` alias.
|
|
59
|
+
|
|
60
|
+
To pin the SDK, use `/static/<version>/loader.js` for a release that includes the loader. An explicitly versioned loader URL keeps loading its versioned sibling `array.js`. Historical releases without a loader are not changed or backfilled. Reverse proxies and self-hosted deployments must serve the versioned SDK directories as well as the loader, along with the SDK's normal API/config/extension paths.
|
|
61
|
+
|
|
62
|
+
Fatal automatic-initialization configuration errors are reported even when debug mode is off. Invalid JSON configuration still prevents automatic initialization.
|
|
63
|
+
|
|
64
|
+
The loader forwards its CSP nonce to the SDK script. Your CSP must still allow the SDK, its extensions, and event ingestion. An existing legacy stub or live global instance takes precedence: the loader never replaces it or switches its project token.
|
|
65
|
+
|
|
14
66
|
## Surveys and capture
|
|
15
67
|
|
|
16
68
|
Built-in surveys stay hidden while event capture is disabled, including before consent when `opt_out_capturing_by_default` is enabled. This applies to automatic display, `displaySurvey()` (including `ignoreConditions`), and `renderSurvey()`. Delayed surveys recheck capture before appearing.
|