vybekiit 0.7.34 → 0.7.35
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/dist/bin.js +520 -9
- package/dist/global-skills/eas-app-stores/SKILL.md +2 -2
- package/dist/global-skills/eas-hosting/SKILL.md +2 -2
- package/dist/global-skills/eas-observe/SKILL.md +7 -15
- package/dist/global-skills/eas-observe/references/queries.md +51 -10
- package/dist/global-skills/eas-observe/references/setup.md +60 -7
- package/dist/global-skills/eas-simulator/SKILL.md +2 -2
- package/dist/global-skills/eas-simulator/references/run-your-app.md +2 -0
- package/dist/global-skills/eas-update/SKILL.md +2 -2
- package/dist/global-skills/eas-update-insights/SKILL.md +2 -2
- package/dist/global-skills/eas-workflows/SKILL.md +2 -2
- package/dist/global-skills/expo-animation/SKILL.md +2 -2
- package/dist/global-skills/expo-app-clip/SKILL.md +2 -2
- package/dist/global-skills/expo-brownfield/SKILL.md +2 -2
- package/dist/global-skills/expo-data-fetching/SKILL.md +2 -2
- package/dist/global-skills/expo-design-system/SKILL.md +2 -2
- package/dist/global-skills/expo-dev-client/SKILL.md +4 -2
- package/dist/global-skills/expo-dom/SKILL.md +2 -2
- package/dist/global-skills/expo-examples/SKILL.md +2 -2
- package/dist/global-skills/expo-migrate-module/SKILL.md +2 -2
- package/dist/global-skills/expo-module/SKILL.md +2 -2
- package/dist/global-skills/expo-native-ui/SKILL.md +2 -2
- package/dist/global-skills/expo-overview/SKILL.md +9 -4
- package/dist/global-skills/expo-overview/references/launch-without-dev-menu.md +24 -0
- package/dist/global-skills/expo-project-structure/SKILL.md +2 -2
- package/dist/global-skills/expo-router/SKILL.md +2 -2
- package/dist/global-skills/expo-ui/SKILL.md +2 -2
- package/dist/global-skills/expo-upgrade/SKILL.md +2 -2
- package/dist/global-skills/expo-web-to-native/SKILL.md +2 -2
- package/dist/global-skills/expo-web-to-native/references/verify-on-device.md +1 -1
- package/dist/global-skills/firebase-app-hosting-basics/SKILL.md +16 -3
- package/dist/global-skills/firebase-app-hosting-basics/references/cli_commands.md +10 -12
- package/dist/global-skills/firebase-app-hosting-basics/references/configuration.md +2 -1
- package/dist/global-skills/firebase-basics/SKILL.md +6 -4
- package/dist/global-skills/firebase-basics/references/web_setup.md +3 -3
- package/dist/global-skills/instrument-feature-flags/references/adding-feature-flag-code.md +81 -333
- package/dist/global-skills/instrument-feature-flags/references/android.md +19 -13
- package/dist/global-skills/instrument-feature-flags/references/api.md +17 -13
- package/dist/global-skills/instrument-feature-flags/references/best-practices.md +35 -35
- package/dist/global-skills/instrument-feature-flags/references/django.md +120 -72
- package/dist/global-skills/instrument-feature-flags/references/dotnet.md +48 -102
- package/dist/global-skills/instrument-feature-flags/references/elixir.md +4 -10
- package/dist/global-skills/instrument-feature-flags/references/flask.md +55 -39
- package/dist/global-skills/instrument-feature-flags/references/flutter.md +19 -13
- package/dist/global-skills/instrument-feature-flags/references/go.md +21 -13
- package/dist/global-skills/instrument-feature-flags/references/ios.md +19 -13
- package/dist/global-skills/instrument-feature-flags/references/java.md +4 -12
- package/dist/global-skills/instrument-feature-flags/references/laravel.md +18 -16
- package/dist/global-skills/instrument-feature-flags/references/next-js.md +44 -54
- package/dist/global-skills/instrument-feature-flags/references/nodejs.md +21 -13
- package/dist/global-skills/instrument-feature-flags/references/php.md +21 -13
- package/dist/global-skills/instrument-feature-flags/references/python.md +22 -14
- package/dist/global-skills/instrument-feature-flags/references/react-native.md +18 -13
- package/dist/global-skills/instrument-feature-flags/references/react.md +21 -14
- package/dist/global-skills/instrument-feature-flags/references/ruby-on-rails.md +31 -68
- package/dist/global-skills/instrument-feature-flags/references/ruby.md +21 -13
- package/dist/global-skills/instrument-feature-flags/references/rust.md +10 -34
- package/dist/global-skills/instrument-feature-flags/references/usage.md +94 -114
- package/dist/global-skills/instrument-feature-flags/references/web.md +21 -13
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-android.md +7 -7
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-expo.md +2 -2
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-fastapi.md +1 -1
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-flask.md +1 -1
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-laravel.md +2 -2
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-php.md +1 -1
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-python.md +1 -1
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-ruby.md +1 -1
- package/dist/global-skills/instrument-product-analytics/references/EXAMPLE-swift.md +4 -4
- package/dist/global-skills/instrument-product-analytics/references/android.md +64 -109
- package/dist/global-skills/instrument-product-analytics/references/angular.md +32 -28
- package/dist/global-skills/instrument-product-analytics/references/astro.md +29 -34
- package/dist/global-skills/instrument-product-analytics/references/configuration.md +27 -42
- package/dist/global-skills/instrument-product-analytics/references/django.md +120 -72
- package/dist/global-skills/instrument-product-analytics/references/dotnet.md +48 -102
- package/dist/global-skills/instrument-product-analytics/references/elixir.md +25 -50
- package/dist/global-skills/instrument-product-analytics/references/flask.md +55 -39
- package/dist/global-skills/instrument-product-analytics/references/flutter.md +94 -96
- package/dist/global-skills/instrument-product-analytics/references/go.md +39 -77
- package/dist/global-skills/instrument-product-analytics/references/identify-users.md +24 -26
- package/dist/global-skills/instrument-product-analytics/references/ios.md +22 -22
- package/dist/global-skills/instrument-product-analytics/references/laravel.md +18 -16
- package/dist/global-skills/instrument-product-analytics/references/next-js.md +44 -54
- package/dist/global-skills/instrument-product-analytics/references/nuxt-js-3-6.md +32 -34
- package/dist/global-skills/instrument-product-analytics/references/nuxt-js.md +36 -36
- package/dist/global-skills/instrument-product-analytics/references/php.md +42 -87
- package/dist/global-skills/instrument-product-analytics/references/posthog-python.md +15 -12
- package/dist/global-skills/instrument-product-analytics/references/python.md +106 -154
- package/dist/global-skills/instrument-product-analytics/references/react-native.md +97 -176
- package/dist/global-skills/instrument-product-analytics/references/react-router-v6.md +29 -40
- package/dist/global-skills/instrument-product-analytics/references/react-router-v7-data-mode.md +29 -38
- package/dist/global-skills/instrument-product-analytics/references/react-router-v7-declarative-mode.md +29 -40
- package/dist/global-skills/instrument-product-analytics/references/react-router-v7-framework-mode.md +32 -37
- package/dist/global-skills/instrument-product-analytics/references/ruby-on-rails.md +31 -68
- package/dist/global-skills/instrument-product-analytics/references/ruby.md +40 -96
- package/dist/global-skills/instrument-product-analytics/references/svelte.md +34 -34
- package/dist/global-skills/instrument-product-analytics/references/tanstack-start.md +19 -19
- package/dist/global-skills/instrument-product-analytics/references/usage.md +94 -114
- package/dist/global-skills/instrument-product-analytics/references/vue-js.md +33 -45
- package/dist/global-skills/mongodb-search-and-ai/SKILL.md +40 -22
- package/dist/global-skills/mongodb-search-and-ai/references/automated-embedding.md +8 -2
- package/dist/global-skills/mongodb-search-and-ai/references/quick-start.md +725 -0
- package/dist/global-skills/mongodb-search-and-ai/scripts/quickstart_complete.py +386 -0
- package/dist/global-skills/neon/references/claimable-neon.md +2 -0
- package/dist/global-skills/neon-realtime/references/queries.md +2 -2
- package/dist/global-skills/resend/SKILL.md +1 -1
- package/dist/global-skills/resend/references/inboxes.md +49 -21
- package/dist/global-skills/resend/references/webhooks.md +1 -1
- package/dist/global-skills/stripe-best-practices/SKILL.md +1 -1
- package/dist/global-skills/stripe-best-practices/references/connect.md +1 -1
- package/dist/global-skills/stripe-best-practices/references/security.md +9 -9
- package/dist/global-skills/vybekiitReferences/backend/AGENTS.md +1 -1
- package/dist/global-skills/vybekiitReferences/backend/agent/goal-routes.json +303 -18
- package/dist/global-skills/vybekiitReferences/extension/AGENTS.md +1 -1
- package/dist/global-skills/vybekiitReferences/extension/agent/goal-routes.json +352 -18
- package/dist/global-skills/vybekiitReferences/mobile/AGENTS.md +1 -1
- package/dist/global-skills/vybekiitReferences/mobile/agent/goal-routes.json +413 -22
- package/dist/global-skills/vybekiitReferences/spa/AGENTS.md +1 -1
- package/dist/global-skills/vybekiitReferences/spa/agent/goal-routes.json +391 -21
- package/dist/global-skills/vybekiitReferences/web/AGENTS.md +14 -2
- package/dist/global-skills/vybekiitReferences/web/agent/goal-routes.json +490 -32
- package/package.json +8 -8
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
# Ruby
|
|
4
4
|
|
|
5
|
+
Canonical URL: https://posthog.com/docs/libraries/ruby
|
|
6
|
+
|
|
7
|
+
> **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.posthog.com` instead of `https://app.posthog.com` (app host for links).
|
|
8
|
+
|
|
9
|
+
## On this page
|
|
10
|
+
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Configuration](#configuration)
|
|
13
|
+
- [Identifying users](#identifying-users)
|
|
14
|
+
- [Capturing events](#capturing-events)
|
|
15
|
+
- [Person profiles and properties](#person-profiles-and-properties)
|
|
16
|
+
- [Alias](#alias)
|
|
17
|
+
- [Feature flags](#feature-flags)
|
|
18
|
+
- [Experiments (A/B tests)](#experiments-ab-tests)
|
|
19
|
+
- [Group analytics](#group-analytics)
|
|
20
|
+
- [Exception capture](#exception-capture)
|
|
21
|
+
- [Debug mode](#debug-mode)
|
|
22
|
+
- [Test helpers](#test-helpers)
|
|
23
|
+
- [Thank you](#thank-you)
|
|
24
|
+
|
|
5
25
|
The `posthog-ruby` library provides tracking functionality on the server-side for applications built in Ruby.
|
|
6
26
|
|
|
7
27
|
It uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server-side application that needs performance.
|
|
@@ -12,16 +32,12 @@ It uses an internal queue to make calls fast and non-blocking. It also batches r
|
|
|
12
32
|
|
|
13
33
|
Add this to your `Gemfile`:
|
|
14
34
|
|
|
15
|
-
Terminal
|
|
16
|
-
|
|
17
35
|
```bash
|
|
18
36
|
gem "posthog-ruby"
|
|
19
37
|
```
|
|
20
38
|
|
|
21
39
|
In your app, set your API key **before** making any calls. If setting a custom `host`, make sure to include the protocol (e.g. `https://`).
|
|
22
40
|
|
|
23
|
-
Ruby
|
|
24
|
-
|
|
25
41
|
```ruby
|
|
26
42
|
require 'posthog'
|
|
27
43
|
|
|
@@ -38,8 +54,6 @@ You can find your project token and instance address in the [project settings](h
|
|
|
38
54
|
|
|
39
55
|
Initialize the client with your project token before making any calls:
|
|
40
56
|
|
|
41
|
-
Ruby
|
|
42
|
-
|
|
43
57
|
```ruby
|
|
44
58
|
require 'posthog'
|
|
45
59
|
|
|
@@ -73,8 +87,6 @@ Available client options:
|
|
|
73
87
|
|
|
74
88
|
Use `before_send` to add, modify, or drop events immediately before the SDK queues or sends them:
|
|
75
89
|
|
|
76
|
-
Ruby
|
|
77
|
-
|
|
78
90
|
```ruby
|
|
79
91
|
posthog = PostHog::Client.new({
|
|
80
92
|
api_key: '<ph_project_token>',
|
|
@@ -92,8 +104,6 @@ posthog = PostHog::Client.new({
|
|
|
92
104
|
|
|
93
105
|
For short-lived scripts, call `flush` before the process exits. Call `shutdown` when your application is stopping to flush pending events and stop background resources.
|
|
94
106
|
|
|
95
|
-
Ruby
|
|
96
|
-
|
|
97
107
|
```ruby
|
|
98
108
|
posthog.capture({ distinct_id: 'user_123', event: 'script_finished' })
|
|
99
109
|
posthog.flush
|
|
@@ -102,14 +112,12 @@ posthog.shutdown
|
|
|
102
112
|
|
|
103
113
|
## Identifying users
|
|
104
114
|
|
|
105
|
-
> **Identifying users is required.** Backend events need a `distinct_id` that matches the ID your frontend uses when calling `posthog.identify()`. Without this, backend events are orphaned — they can't be linked to frontend event captures, [session replays](/docs/session-replay.md), [LLM traces](/docs/ai-engineering.md), or [error tracking](/docs/error-tracking.md).
|
|
115
|
+
> **Identifying users is required.** Backend events need a `distinct_id` that matches the ID your frontend uses when calling `posthog.identify()`. Without this, backend events are orphaned — they can't be linked to frontend event captures, [session replays](https://posthog.com/docs/session-replay.md), [LLM traces](https://posthog.com/docs/ai-engineering.md), or [error tracking](https://posthog.com/docs/error-tracking.md).
|
|
106
116
|
>
|
|
107
|
-
> See our guide on [identifying users](/docs/getting-started/identify-users.md) for how to set this up.
|
|
117
|
+
> See our guide on [identifying users](https://posthog.com/docs/getting-started/identify-users.md) for how to set this up.
|
|
108
118
|
|
|
109
119
|
Identify a user and set their person properties with `identify`:
|
|
110
120
|
|
|
111
|
-
Ruby
|
|
112
|
-
|
|
113
121
|
```ruby
|
|
114
122
|
posthog.identify({
|
|
115
123
|
distinct_id: 'distinct_id_of_your_user',
|
|
@@ -124,8 +132,6 @@ posthog.identify({
|
|
|
124
132
|
|
|
125
133
|
You can send custom events using `capture`:
|
|
126
134
|
|
|
127
|
-
Ruby
|
|
128
|
-
|
|
129
135
|
```ruby
|
|
130
136
|
posthog.capture({
|
|
131
137
|
distinct_id: 'distinct_id_of_the_user',
|
|
@@ -137,9 +143,7 @@ posthog.capture({
|
|
|
137
143
|
|
|
138
144
|
### Setting event properties
|
|
139
145
|
|
|
140
|
-
Optionally, you can include additional information with the event by including a [properties](/docs/data/events.md#event-properties) object:
|
|
141
|
-
|
|
142
|
-
Ruby
|
|
146
|
+
Optionally, you can include additional information with the event by including a [properties](https://posthog.com/docs/data/events.md#event-properties) object:
|
|
143
147
|
|
|
144
148
|
```ruby
|
|
145
149
|
posthog.capture({
|
|
@@ -156,8 +160,6 @@ posthog.capture({
|
|
|
156
160
|
|
|
157
161
|
If you're aiming for a backend-only implementation of PostHog and won't be capturing events from your frontend, you can send `pageviews` from your backend like so:
|
|
158
162
|
|
|
159
|
-
Ruby
|
|
160
|
-
|
|
161
163
|
```ruby
|
|
162
164
|
posthog.capture({
|
|
163
165
|
distinct_id: 'distinct_id_of_the_user',
|
|
@@ -184,9 +186,7 @@ posthog.capture({
|
|
|
184
186
|
|
|
185
187
|
## Person profiles and properties
|
|
186
188
|
|
|
187
|
-
The Ruby SDK captures identified events by default. These create [person profiles](/docs/data/persons.md). To set [person properties](/docs/product-analytics/person-properties.md) in these profiles, include them when capturing an event:
|
|
188
|
-
|
|
189
|
-
Ruby
|
|
189
|
+
The Ruby SDK captures identified events by default. These create [person profiles](https://posthog.com/docs/data/persons.md). To set [person properties](https://posthog.com/docs/product-analytics/person-properties.md) in these profiles, include them when capturing an event:
|
|
190
190
|
|
|
191
191
|
```ruby
|
|
192
192
|
posthog.capture({
|
|
@@ -199,11 +199,9 @@ posthog.capture({
|
|
|
199
199
|
})
|
|
200
200
|
```
|
|
201
201
|
|
|
202
|
-
For more details on the difference between `$set` and `$set_once`, see our [person properties docs](/docs/product-analytics/person-properties.md#what-is-the-difference-between-set-and-set_once).
|
|
203
|
-
|
|
204
|
-
To capture [anonymous events](/docs/data/anonymous-vs-identified-events.md) without person profiles, set the event's `$process_person_profile` property to `false`:
|
|
202
|
+
For more details on the difference between `$set` and `$set_once`, see our [person properties docs](https://posthog.com/docs/product-analytics/person-properties.md#what-is-the-difference-between-set-and-set_once).
|
|
205
203
|
|
|
206
|
-
|
|
204
|
+
To capture [anonymous events](https://posthog.com/docs/data/anonymous-vs-identified-events.md) without person profiles, set the event's `$process_person_profile` property to `false`:
|
|
207
205
|
|
|
208
206
|
```ruby
|
|
209
207
|
posthog.capture({
|
|
@@ -221,8 +219,6 @@ Sometimes, you want to assign multiple distinct IDs to a single user. This is he
|
|
|
221
219
|
|
|
222
220
|
In this case, you can use `alias` to assign another distinct ID to the same user.
|
|
223
221
|
|
|
224
|
-
Ruby
|
|
225
|
-
|
|
226
222
|
```ruby
|
|
227
223
|
posthog.alias({
|
|
228
224
|
distinct_id: 'distinct_id',
|
|
@@ -230,11 +226,11 @@ posthog.alias({
|
|
|
230
226
|
})
|
|
231
227
|
```
|
|
232
228
|
|
|
233
|
-
We strongly recommend reading our docs on [alias](/docs/product-analytics/identify.md#alias-assigning-multiple-distinct-ids-to-the-same-user) to best understand how to correctly use this method.
|
|
229
|
+
We strongly recommend reading our docs on [alias](https://posthog.com/docs/product-analytics/identify.md#alias-assigning-multiple-distinct-ids-to-the-same-user) to best understand how to correctly use this method.
|
|
234
230
|
|
|
235
231
|
## Feature flags
|
|
236
232
|
|
|
237
|
-
PostHog's [feature flags](/docs/feature-flags.md) enable you to safely deploy and roll back new features as well as target specific users and groups with them.
|
|
233
|
+
PostHog's [feature flags](https://posthog.com/docs/feature-flags.md) enable you to safely deploy and roll back new features as well as target specific users and groups with them.
|
|
238
234
|
|
|
239
235
|
There are two steps to implement feature flags in Ruby:
|
|
240
236
|
|
|
@@ -244,8 +240,6 @@ Call `posthog.evaluate_flags()` once for the user, then read values from the ret
|
|
|
244
240
|
|
|
245
241
|
#### Boolean feature flags
|
|
246
242
|
|
|
247
|
-
Ruby
|
|
248
|
-
|
|
249
243
|
```ruby
|
|
250
244
|
flags = posthog.evaluate_flags('distinct_id_of_your_user')
|
|
251
245
|
|
|
@@ -258,8 +252,6 @@ end
|
|
|
258
252
|
|
|
259
253
|
#### Multivariate feature flags
|
|
260
254
|
|
|
261
|
-
Ruby
|
|
262
|
-
|
|
263
255
|
```ruby
|
|
264
256
|
flags = posthog.evaluate_flags('distinct_id_of_your_user')
|
|
265
257
|
|
|
@@ -278,9 +270,9 @@ end
|
|
|
278
270
|
|
|
279
271
|
### Step 2: Include feature flag information when capturing events
|
|
280
272
|
|
|
281
|
-
If you want use your feature flag to breakdown or filter events in your [insights](/docs/product-analytics/insights.md), you'll need to include feature flag information in those events. This ensures that the feature flag value is attributed correctly to the event.
|
|
273
|
+
If you want use your feature flag to breakdown or filter events in your [insights](https://posthog.com/docs/product-analytics/insights.md), you'll need to include feature flag information in those events. This ensures that the feature flag value is attributed correctly to the event.
|
|
282
274
|
|
|
283
|
-
> **Note:** This step is only required for events captured using our server-side SDKs or [API](/docs/api.md).
|
|
275
|
+
> **Note:** This step is only required for events captured using our server-side SDKs or [API](https://posthog.com/docs/api.md).
|
|
284
276
|
|
|
285
277
|
There are two methods you can use to include feature flag information in your events:
|
|
286
278
|
|
|
@@ -288,8 +280,6 @@ There are two methods you can use to include feature flag information in your ev
|
|
|
288
280
|
|
|
289
281
|
Pass the same `flags` object that you used for branching. This attaches the exact flag values from that evaluation and doesn't make another `/flags` request.
|
|
290
282
|
|
|
291
|
-
Ruby
|
|
292
|
-
|
|
293
283
|
```ruby
|
|
294
284
|
flags = posthog.evaluate_flags('distinct_id_of_your_user')
|
|
295
285
|
|
|
@@ -308,8 +298,6 @@ By default, this attaches every flag in the snapshot using `$feature/<flag-key>`
|
|
|
308
298
|
|
|
309
299
|
To reduce event property bloat, pass a filtered snapshot:
|
|
310
300
|
|
|
311
|
-
Ruby
|
|
312
|
-
|
|
313
301
|
```ruby
|
|
314
302
|
# Attach only flags accessed with enabled?() or get_flag() before this call
|
|
315
303
|
posthog.capture({
|
|
@@ -332,8 +320,6 @@ posthog.capture({
|
|
|
332
320
|
|
|
333
321
|
In the event properties, include `$feature/feature_flag_name: variant_key`:
|
|
334
322
|
|
|
335
|
-
Ruby
|
|
336
|
-
|
|
337
323
|
```ruby
|
|
338
324
|
posthog.capture({
|
|
339
325
|
distinct_id: 'distinct_id_of_your_user',
|
|
@@ -349,8 +335,6 @@ posthog.capture({
|
|
|
349
335
|
|
|
350
336
|
By default, `evaluate_flags()` evaluates every flag for the user. If you only need a few flags, pass `flag_keys` to request only those flags:
|
|
351
337
|
|
|
352
|
-
Ruby
|
|
353
|
-
|
|
354
338
|
```ruby
|
|
355
339
|
flags = posthog.evaluate_flags(
|
|
356
340
|
'distinct_id_of_your_user',
|
|
@@ -362,8 +346,6 @@ flags = posthog.evaluate_flags(
|
|
|
362
346
|
|
|
363
347
|
If you want to skip the remote `/flags` request and only use locally cached definitions, pass `only_evaluate_locally: true`:
|
|
364
348
|
|
|
365
|
-
Ruby
|
|
366
|
-
|
|
367
349
|
```ruby
|
|
368
350
|
flags = posthog.evaluate_flags(
|
|
369
351
|
'distinct_id_of_your_user',
|
|
@@ -375,8 +357,6 @@ flags = posthog.evaluate_flags(
|
|
|
375
357
|
|
|
376
358
|
Pass `disable_geoip: true` to disable GeoIP lookup for remote flag evaluation:
|
|
377
359
|
|
|
378
|
-
Ruby
|
|
379
|
-
|
|
380
360
|
```ruby
|
|
381
361
|
flags = posthog.evaluate_flags(
|
|
382
362
|
'distinct_id_of_your_user',
|
|
@@ -386,7 +366,7 @@ flags = posthog.evaluate_flags(
|
|
|
386
366
|
|
|
387
367
|
### Sending `$feature_flag_called` events
|
|
388
368
|
|
|
389
|
-
Capturing `$feature_flag_called` events enables PostHog to know when a flag was accessed by a user and provide [analytics and insights](/docs/product-analytics/insights.md) on the flag. With `evaluate_flags()`, the SDK sends this event when you call `flags.enabled?()` or `flags.get_flag()` for a flag.
|
|
369
|
+
Capturing `$feature_flag_called` events enables PostHog to know when a flag was accessed by a user and provide [analytics and insights](https://posthog.com/docs/product-analytics/insights.md) on the flag. With `evaluate_flags()`, the SDK sends this event when you call `flags.enabled?()` or `flags.get_flag()` for a flag.
|
|
390
370
|
|
|
391
371
|
The SDK deduplicates these events per `(distinct_id, flag, value)` in a local cache. If you reinitialize the PostHog client, the cache resets and `$feature_flag_called` events may be sent again. PostHog handles duplicates, so duplicate `$feature_flag_called` events don't affect your analytics.
|
|
392
372
|
|
|
@@ -394,14 +374,12 @@ The SDK deduplicates these events per `(distinct_id, flag, value)` in a local ca
|
|
|
394
374
|
|
|
395
375
|
### Advanced: Overriding server properties
|
|
396
376
|
|
|
397
|
-
Sometimes, you may want to evaluate feature flags using [person properties](/docs/product-analytics/person-properties.md), [groups](/docs/product-analytics/group-analytics.md), or group properties that haven't been ingested yet, or were set incorrectly earlier.
|
|
377
|
+
Sometimes, you may want to evaluate feature flags using [person properties](https://posthog.com/docs/product-analytics/person-properties.md), [groups](https://posthog.com/docs/product-analytics/group-analytics.md), or group properties that haven't been ingested yet, or were set incorrectly earlier.
|
|
398
378
|
|
|
399
379
|
You can provide properties to evaluate the flag with by using the `person properties`, `groups`, and `group properties` arguments. PostHog will then use these values to evaluate the flag, instead of any properties currently stored on your PostHog server.
|
|
400
380
|
|
|
401
381
|
For example:
|
|
402
382
|
|
|
403
|
-
Ruby
|
|
404
|
-
|
|
405
383
|
```ruby
|
|
406
384
|
flags = posthog.evaluate_flags(
|
|
407
385
|
'distinct_id_of_the_user',
|
|
@@ -458,8 +436,6 @@ Simply include any of these properties in the `person_properties` parameter alon
|
|
|
458
436
|
|
|
459
437
|
You can configure the `feature_flag_request_timeout_seconds` parameter when initializing your PostHog client to set a flag request timeout. This helps prevent your code from being blocked if PostHog's servers are too slow to respond. By default, this is set to 3 seconds.
|
|
460
438
|
|
|
461
|
-
Ruby
|
|
462
|
-
|
|
463
439
|
```ruby
|
|
464
440
|
posthog = PostHog::Client.new({
|
|
465
441
|
# rest of your configuration...
|
|
@@ -485,14 +461,12 @@ Evaluating feature flags requires making a request to PostHog for each flag. How
|
|
|
485
461
|
|
|
486
462
|
It is best practice to use local evaluation flags when possible, since this enables you to resolve flags faster and with fewer API calls.
|
|
487
463
|
|
|
488
|
-
For details on how to implement local evaluation, see our [local evaluation guide](/docs/feature-flags/local-evaluation.md).
|
|
464
|
+
For details on how to implement local evaluation, see our [local evaluation guide](https://posthog.com/docs/feature-flags/local-evaluation.md).
|
|
489
465
|
|
|
490
466
|
#### Evaluating feature flags locally in unicorn server
|
|
491
467
|
|
|
492
468
|
If you have `preload_app true` in your unicorn config, you can use the [`after_fork`](https://www.rubydoc.info/gems/unicorn/Unicorn%2FConfigurator:after_fork) hook (which is part of the unicorn's configuration) to enable the feature flag cache to receive the updates from PostHog.
|
|
493
469
|
|
|
494
|
-
Ruby
|
|
495
|
-
|
|
496
470
|
```ruby
|
|
497
471
|
after_fork do |_server, _worker|
|
|
498
472
|
$posthog = PostHog::Client.new({
|
|
@@ -508,8 +482,6 @@ end
|
|
|
508
482
|
|
|
509
483
|
If you use Puma with multiple workers, you can use the `on_worker_boot` hook (which is part of Puma's configuration) to enable the feature flag cache to receive updates from PostHog.
|
|
510
484
|
|
|
511
|
-
Ruby
|
|
512
|
-
|
|
513
485
|
```ruby
|
|
514
486
|
on_worker_boot do
|
|
515
487
|
$posthog = PostHog::Client.new({
|
|
@@ -530,8 +502,6 @@ end
|
|
|
530
502
|
- `on_flag_definitions_received(data)` – stores freshly fetched definitions.
|
|
531
503
|
- `shutdown` – releases locks or other resources.
|
|
532
504
|
|
|
533
|
-
Ruby
|
|
534
|
-
|
|
535
505
|
```ruby
|
|
536
506
|
posthog = PostHog::Client.new({
|
|
537
507
|
api_key: '<ph_project_token>',
|
|
@@ -544,17 +514,13 @@ posthog = PostHog::Client.new({
|
|
|
544
514
|
|
|
545
515
|
Use `get_remote_config_payload` to fetch the decrypted remote config payload for a flag. This requires `personal_api_key`.
|
|
546
516
|
|
|
547
|
-
Ruby
|
|
548
|
-
|
|
549
517
|
```ruby
|
|
550
518
|
payload = posthog.get_remote_config_payload('flag-key')
|
|
551
519
|
```
|
|
552
520
|
|
|
553
521
|
## Experiments (A/B tests)
|
|
554
522
|
|
|
555
|
-
Since [experiments](/docs/experiments/start-here.md) use feature flags, the code for running an experiment is very similar to the feature flags code:
|
|
556
|
-
|
|
557
|
-
Ruby
|
|
523
|
+
Since [experiments](https://posthog.com/docs/experiments/start-here.md) use feature flags, the code for running an experiment is very similar to the feature flags code:
|
|
558
524
|
|
|
559
525
|
```ruby
|
|
560
526
|
flags = posthog.evaluate_flags('user_distinct_id')
|
|
@@ -565,18 +531,16 @@ if variant == 'variant-name'
|
|
|
565
531
|
end
|
|
566
532
|
```
|
|
567
533
|
|
|
568
|
-
It's also possible to [run experiments without using feature flags](/docs/experiments/running-experiments-without-feature-flags.md).
|
|
534
|
+
It's also possible to [run experiments without using feature flags](https://posthog.com/docs/experiments/running-experiments-without-feature-flags.md).
|
|
569
535
|
|
|
570
536
|
## Group analytics
|
|
571
537
|
|
|
572
|
-
Group analytics allows you to associate an event with a group (e.g. teams, organizations, etc.). Read the [Group Analytics](/docs/user-guides/group-analytics.md) guide for more information.
|
|
538
|
+
Group analytics allows you to associate an event with a group (e.g. teams, organizations, etc.). Read the [Group Analytics](https://posthog.com/docs/user-guides/group-analytics.md) guide for more information.
|
|
573
539
|
|
|
574
|
-
> **Note:** This is a paid feature and is not available on the open-source or free cloud plan. Learn more on the [pricing page](/pricing.md).
|
|
540
|
+
> **Note:** This is a paid feature and is not available on the open-source or free cloud plan. Learn more on the [pricing page](https://posthog.com/pricing.md).
|
|
575
541
|
|
|
576
542
|
Capture an event and associate it with a group:
|
|
577
543
|
|
|
578
|
-
Ruby
|
|
579
|
-
|
|
580
544
|
```ruby
|
|
581
545
|
posthog.capture({
|
|
582
546
|
distinct_id: 'distinct_id_of_the_user',
|
|
@@ -593,8 +557,6 @@ posthog.capture({
|
|
|
593
557
|
|
|
594
558
|
Update properties on a group:
|
|
595
559
|
|
|
596
|
-
Ruby
|
|
597
|
-
|
|
598
560
|
```ruby
|
|
599
561
|
posthog.group_identify({
|
|
600
562
|
group_type: 'company',
|
|
@@ -611,16 +573,14 @@ If the optional `distinct_id` is not provided in the group identify call, it def
|
|
|
611
573
|
|
|
612
574
|
## Exception capture
|
|
613
575
|
|
|
614
|
-
You can capture exceptions using the `posthog-ruby` library. This enables you to see stack traces and debug errors in your application. Learn more in our [error tracking docs](/docs/error-tracking/installation/ruby.md).
|
|
576
|
+
You can capture exceptions using the `posthog-ruby` library. This enables you to see stack traces and debug errors in your application. Learn more in our [error tracking docs](https://posthog.com/docs/error-tracking/installation/ruby.md).
|
|
615
577
|
|
|
616
578
|
**Using Rails?**
|
|
617
579
|
|
|
618
|
-
The [posthog-rails](/docs/libraries/ruby-on-rails.md) gem provides automatic exception capture, ActiveJob instrumentation, and user context out of the box. See our [Rails error tracking guide](/docs/error-tracking/installation/ruby-on-rails.md) for details.
|
|
580
|
+
The [posthog-rails](https://posthog.com/docs/libraries/ruby-on-rails.md) gem provides automatic exception capture, ActiveJob instrumentation, and user context out of the box. See our [Rails error tracking guide](https://posthog.com/docs/error-tracking/installation/ruby-on-rails.md) for details.
|
|
619
581
|
|
|
620
582
|
For non-Rails Ruby applications, you can manually capture exceptions with `capture_exception`:
|
|
621
583
|
|
|
622
|
-
Ruby
|
|
623
|
-
|
|
624
584
|
```ruby
|
|
625
585
|
begin
|
|
626
586
|
# Code that might raise an exception
|
|
@@ -645,9 +605,7 @@ The `capture_exception` method accepts the following parameters:
|
|
|
645
605
|
| `additional_properties` | `Hash` | Additional properties to attach to the exception event. Optional. |
|
|
646
606
|
| `flags` | `PostHog::FeatureFlagEvaluations` | Optional keyword argument. Adds the same feature flag properties as `capture({ flags: flags })`. |
|
|
647
607
|
|
|
648
|
-
You can also override the [fingerprint](/docs/error-tracking/fingerprints.md) to customize how exceptions are grouped into issues:
|
|
649
|
-
|
|
650
|
-
Ruby
|
|
608
|
+
You can also override the [fingerprint](https://posthog.com/docs/error-tracking/fingerprints.md) to customize how exceptions are grouped into issues:
|
|
651
609
|
|
|
652
610
|
```ruby
|
|
653
611
|
posthog.capture_exception(
|
|
@@ -663,16 +621,12 @@ posthog.capture_exception(
|
|
|
663
621
|
|
|
664
622
|
The Ruby SDK logs warnings by default. You can change the log level to `DEBUG` to debug the client:
|
|
665
623
|
|
|
666
|
-
Ruby
|
|
667
|
-
|
|
668
624
|
```ruby
|
|
669
625
|
posthog.logger.level = Logger::DEBUG
|
|
670
626
|
```
|
|
671
627
|
|
|
672
628
|
You can also replace the SDK logger globally:
|
|
673
629
|
|
|
674
|
-
Ruby
|
|
675
|
-
|
|
676
630
|
```ruby
|
|
677
631
|
PostHog::Logging.logger = Rails.logger
|
|
678
632
|
```
|
|
@@ -681,8 +635,6 @@ PostHog::Logging.logger = Rails.logger
|
|
|
681
635
|
|
|
682
636
|
When `test_mode: true`, events remain queued. You can inspect and clear the queue in tests:
|
|
683
637
|
|
|
684
|
-
Ruby
|
|
685
|
-
|
|
686
638
|
```ruby
|
|
687
639
|
posthog = PostHog::Client.new({ api_key: '<ph_project_token>', test_mode: true })
|
|
688
640
|
posthog.capture({ distinct_id: 'user_123', event: 'test_event' })
|
|
@@ -695,11 +647,3 @@ posthog.clear
|
|
|
695
647
|
## Thank you
|
|
696
648
|
|
|
697
649
|
This library is largely based on the `analytics-ruby` package.
|
|
698
|
-
|
|
699
|
-
### Still have questions?
|
|
700
|
-
|
|
701
|
-
Ask PostHog AI
|
|
702
|
-
|
|
703
|
-
### Was this page useful?
|
|
704
|
-
|
|
705
|
-
HelpfulCould be better
|
|
@@ -2,18 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
# Svelte
|
|
4
4
|
|
|
5
|
+
Canonical URL: https://posthog.com/docs/libraries/svelte
|
|
6
|
+
|
|
7
|
+
> **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host), and use `https://eu.posthog.com` instead of `https://us.posthog.com` (app host for links).
|
|
8
|
+
|
|
9
|
+
## On this page
|
|
10
|
+
|
|
11
|
+
- [Beta: integration via LLM](#beta-integration-via-llm)
|
|
12
|
+
- [Client-side setup](#client-side-setup)
|
|
13
|
+
- [Identifying users](#identifying-users)
|
|
14
|
+
- [Server-side setup](#server-side-setup)
|
|
15
|
+
- [Feature flags](#feature-flags)
|
|
16
|
+
- [Configuring session replay for server-side rendered apps](#configuring-session-replay-for-server-side-rendered-apps)
|
|
17
|
+
- [Next steps](#next-steps)
|
|
18
|
+
|
|
5
19
|
PostHog makes it easy to get data about traffic and usage of your [Svelte](https://svelte.dev/) app. Integrating PostHog into your site enables analytics about user behavior, custom events capture, session recordings, feature flags, and more.
|
|
6
20
|
|
|
7
|
-
This guide walks you through integrating PostHog into your SvelteKit app using the [JavaScript Web](/docs/libraries/js.md) and [Node.js](/docs/libraries/node.md) SDKs.
|
|
21
|
+
This guide walks you through integrating PostHog into your SvelteKit app using the [JavaScript Web](https://posthog.com/docs/libraries/js.md) and [Node.js](https://posthog.com/docs/libraries/node.md) SDKs.
|
|
8
22
|
|
|
9
23
|
## Beta: integration via LLM
|
|
10
24
|
|
|
11
|
-
Install PostHog for Svelte in seconds with our wizard by running this prompt with [LLM coding agents](/blog/envoy-wizard-llm-agent.md) like Cursor and Bolt, or by running it in your terminal.
|
|
25
|
+
Install PostHog for Svelte in seconds with our wizard by running this prompt with [LLM coding agents](https://posthog.com/blog/envoy-wizard-llm-agent.md) like Cursor and Bolt, or by running it in your terminal.
|
|
12
26
|
|
|
13
27
|
`npx @posthog/wizard`
|
|
14
28
|
|
|
15
|
-
[Learn more](/wizard.md)
|
|
16
|
-
|
|
17
29
|
Or, to integrate manually, continue with the rest of this guide.
|
|
18
30
|
|
|
19
31
|
## Client-side setup
|
|
@@ -52,7 +64,7 @@ bun add posthog-js
|
|
|
52
64
|
> worker-src 'self' blob: data:;
|
|
53
65
|
> ```
|
|
54
66
|
>
|
|
55
|
-
> `script-src` covers the snippet and the lazy-loaded bundles, `connect-src` covers event ingestion and feature flags, and `worker-src` covers session replay. The [toolbar needs a few more](/docs/advanced/content-security-policy.md), or use a [reverse proxy](/docs/advanced/proxy.md) so everything is first-party. Failing to do so causes silent failures where `capture` and `identify` calls never send, so the integration looks complete while zero events arrive. Remember `connect-src` falls back to `default-src`, so `default-src 'self'` blocks event delivery even when the script itself is bundled.
|
|
67
|
+
> `script-src` covers the snippet and the lazy-loaded bundles, `connect-src` covers event ingestion and feature flags, and `worker-src` covers session replay. The [toolbar needs a few more](https://posthog.com/docs/advanced/content-security-policy.md), or use a [reverse proxy](https://posthog.com/docs/advanced/proxy.md) so everything is first-party. Failing to do so causes silent failures where `capture` and `identify` calls never send, so the integration looks complete while zero events arrive. Remember `connect-src` falls back to `default-src`, so `default-src 'self'` blocks event delivery even when the script itself is bundled.
|
|
56
68
|
|
|
57
69
|
Then, if you haven't created a root [layout](https://kit.svelte.dev/docs/routing#layout) already, create a new file called `+layout.js` in your `src/routes` folder In this file, check the environment is the browser, and initialize PostHog if so. You can get both your API key and instance address in your [project settings](https://us.posthog.com/project/settings).
|
|
58
70
|
|
|
@@ -76,18 +88,16 @@ export const load = async () => {
|
|
|
76
88
|
|
|
77
89
|
## Identifying users
|
|
78
90
|
|
|
79
|
-
> **Identifying users is required.** Call `posthog.identify('your-user-id')` after login to link events to a known user. This is what connects frontend event captures, [session replays](/docs/session-replay.md), [LLM traces](/docs/ai-engineering.md), and [error tracking](/docs/error-tracking.md) to the same person — and lets backend events link back too.
|
|
91
|
+
> **Identifying users is required.** Call `posthog.identify('your-user-id')` after login to link events to a known user. This is what connects frontend event captures, [session replays](https://posthog.com/docs/session-replay.md), [LLM traces](https://posthog.com/docs/ai-engineering.md), and [error tracking](https://posthog.com/docs/error-tracking.md) to the same person — and lets backend events link back too.
|
|
80
92
|
>
|
|
81
93
|
> Use a stable ID from your auth system when possible, not an email or display name. Send those as person properties instead. If your app has no other stable key, email works as a fallback if they are unique. Never a shared literal like `"anonymous"` or `"user"`, which pools many people onto one person and corrupts their data. When no ID is available at all, skip the identify and retain the anonymous distinct ID that's automatically assigned.
|
|
82
94
|
>
|
|
83
95
|
> Call `posthog.reset()` on logout, so the next person to use the browser doesn't inherit the last one's identity.
|
|
84
96
|
>
|
|
85
|
-
> See our guide on [identifying users](/docs/getting-started/identify-users.md) for how to set this up.
|
|
97
|
+
> See our guide on [identifying users](https://posthog.com/docs/getting-started/identify-users.md) for how to set this up.
|
|
86
98
|
|
|
87
99
|
If your app calls your own backend, `tracing_headers` adds `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` to matching `fetch` and `XMLHttpRequest` requests. This lets server-side SDKs link backend events, errors, and LLM traces back to frontend sessions and replays. Use hostnames only, without protocols or paths.
|
|
88
100
|
|
|
89
|
-
JavaScript
|
|
90
|
-
|
|
91
101
|
```javascript
|
|
92
102
|
posthog.init('<ph_project_token>', {
|
|
93
103
|
api_host: 'https://us.i.posthog.com',
|
|
@@ -100,25 +110,25 @@ This works in local development too, but match on the hostname alone: use `'loca
|
|
|
100
110
|
|
|
101
111
|
Tracing headers help you attribute events across front and backend consistently. When this isn't available, use your server-side stable IDs to deduce the matching `distinctId`, and pass it in when capturing the event.
|
|
102
112
|
|
|
103
|
-
> ❗️ If you intend on using session replays with a server-side rendered Svelte app ensure that your [asset URLs are configured to be relative](/docs/session-replay/troubleshooting.md#ensure-assets-are-imported-from-the-base-URL-in-Svelte).
|
|
113
|
+
> ❗️ If you intend on using session replays with a server-side rendered Svelte app ensure that your [asset URLs are configured to be relative](https://posthog.com/docs/session-replay/troubleshooting.md#ensure-assets-are-imported-from-the-base-URL-in-Svelte).
|
|
104
114
|
|
|
105
|
-
Set up a reverse proxy (recommended)
|
|
115
|
+
**Set up a reverse proxy (recommended)**
|
|
106
116
|
|
|
107
|
-
We recommend [setting up a reverse proxy](/docs/advanced/proxy.md), so that events are less likely to be intercepted by tracking blockers.
|
|
117
|
+
We recommend [setting up a reverse proxy](https://posthog.com/docs/advanced/proxy.md), so that events are less likely to be intercepted by tracking blockers.
|
|
108
118
|
|
|
109
|
-
We have our [own managed reverse proxy service](/docs/advanced/proxy/managed-reverse-proxy.md), which is free for all PostHog Cloud users, routes through our infrastructure, and makes setting up your proxy easy.
|
|
119
|
+
We have our [own managed reverse proxy service](https://posthog.com/docs/advanced/proxy/managed-reverse-proxy.md), which is free for all PostHog Cloud users, routes through our infrastructure, and makes setting up your proxy easy.
|
|
110
120
|
|
|
111
|
-
If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using [Cloudflare](/docs/advanced/proxy/cloudflare.md), [AWS Cloudfront](/docs/advanced/proxy/cloudfront.md), and [Vercel](/docs/advanced/proxy/vercel.md).
|
|
121
|
+
If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using [Cloudflare](https://posthog.com/docs/advanced/proxy/cloudflare.md), [AWS Cloudfront](https://posthog.com/docs/advanced/proxy/cloudfront.md), and [Vercel](https://posthog.com/docs/advanced/proxy/vercel.md).
|
|
112
122
|
|
|
113
|
-
Grouping products in one project (recommended)
|
|
123
|
+
**Grouping products in one project (recommended)**
|
|
114
124
|
|
|
115
|
-
If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and [group them in one project](/docs/settings/projects.md).
|
|
125
|
+
If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and [group them in one project](https://posthog.com/docs/settings/projects.md).
|
|
116
126
|
|
|
117
127
|
This makes it possible to track users across their entire journey (e.g. from visiting your marketing website to signing up for your product), or how they use your product across multiple platforms.
|
|
118
128
|
|
|
119
|
-
Add IPs to Firewall/WAF allowlists (recommended)
|
|
129
|
+
**Add IPs to Firewall/WAF allowlists (recommended)**
|
|
120
130
|
|
|
121
|
-
For certain features like [heatmaps](/docs/toolbar/heatmaps.md), your Web Application Firewall (WAF) may be blocking PostHog's requests to your site. Add these IP addresses to your WAF allowlist or rules to let PostHog access your site.
|
|
131
|
+
For certain features like [heatmaps](https://posthog.com/docs/toolbar/heatmaps.md), your Web Application Firewall (WAF) may be blocking PostHog's requests to your site. Add these IP addresses to your WAF allowlist or rules to let PostHog access your site.
|
|
122
132
|
|
|
123
133
|
**EU**: `3.75.65.221`, `18.197.246.42`, `3.120.223.253`
|
|
124
134
|
|
|
@@ -128,7 +138,7 @@ These are public, stable IPs used by PostHog services.
|
|
|
128
138
|
|
|
129
139
|
PostHog captures heatmap screenshots using [Browserless](https://www.browserless.io), which has its own IP addresses. Browserless [publishes the current list here](https://docs.browserless.io/baas/troubleshooting/whitelisting-ips).
|
|
130
140
|
|
|
131
|
-
An allowlist does not help when your app has a private address. For apps on an internal network, see [internal and intranet applications](/docs/session-replay/troubleshooting.md#internal-and-intranet-applications).
|
|
141
|
+
An allowlist does not help when your app has a private address. For apps on an internal network, see [internal and intranet applications](https://posthog.com/docs/session-replay/troubleshooting.md#internal-and-intranet-applications).
|
|
132
142
|
|
|
133
143
|
## Server-side setup
|
|
134
144
|
|
|
@@ -231,7 +241,7 @@ export async function load() {
|
|
|
231
241
|
}
|
|
232
242
|
```
|
|
233
243
|
|
|
234
|
-
See our [JavaScript Web](/docs/libraries/js/usage.md#feature-flags) and [Node](/docs/libraries/node.md#feature-flags) docs for more details.
|
|
244
|
+
See our [JavaScript Web](https://posthog.com/docs/libraries/js/usage.md#feature-flags) and [Node](https://posthog.com/docs/libraries/node.md#feature-flags) docs for more details.
|
|
235
245
|
|
|
236
246
|
## Configuring session replay for server-side rendered apps
|
|
237
247
|
|
|
@@ -239,8 +249,6 @@ By default, [Svelte uses relative asset paths](https://kit.svelte.dev/docs/confi
|
|
|
239
249
|
|
|
240
250
|
To fix this, set the config to not use relative paths in `svelte.config.js`:
|
|
241
251
|
|
|
242
|
-
JavaScript
|
|
243
|
-
|
|
244
252
|
```javascript
|
|
245
253
|
kit: {
|
|
246
254
|
paths: {
|
|
@@ -251,18 +259,10 @@ kit: {
|
|
|
251
259
|
|
|
252
260
|
## Next steps
|
|
253
261
|
|
|
254
|
-
For any technical questions for how to integrate specific PostHog features into Svelte (such as analytics, feature flags, A/B testing, surveys, etc.), have a look at our [JavaScript Web](/docs/libraries/js/usage.md) and [Node](/docs/libraries/node.md) SDK docs.
|
|
262
|
+
For any technical questions for how to integrate specific PostHog features into Svelte (such as analytics, feature flags, A/B testing, surveys, etc.), have a look at our [JavaScript Web](https://posthog.com/docs/libraries/js/usage.md) and [Node](https://posthog.com/docs/libraries/node.md) SDK docs.
|
|
255
263
|
|
|
256
264
|
Alternatively, the following tutorials can help you get started:
|
|
257
265
|
|
|
258
|
-
- [How to set up Svelte analytics, feature flags, and more](/tutorials/svelte-analytics.md)
|
|
259
|
-
- [How to set up A/B tests in Svelte](/tutorials/svelte-ab-tests.md)
|
|
260
|
-
- [How to set up surveys in Svelte](/tutorials/svelte-surveys.md)
|
|
261
|
-
|
|
262
|
-
### Still have questions?
|
|
263
|
-
|
|
264
|
-
Ask PostHog AI
|
|
265
|
-
|
|
266
|
-
### Was this page useful?
|
|
267
|
-
|
|
268
|
-
HelpfulCould be better
|
|
266
|
+
- [How to set up Svelte analytics, feature flags, and more](https://posthog.com/tutorials/svelte-analytics.md)
|
|
267
|
+
- [How to set up A/B tests in Svelte](https://posthog.com/tutorials/svelte-ab-tests.md)
|
|
268
|
+
- [How to set up surveys in Svelte](https://posthog.com/tutorials/svelte-surveys.md)
|
|
@@ -2,35 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
# TanStack Start
|
|
4
4
|
|
|
5
|
+
Canonical URL: https://posthog.com/docs/libraries/tanstack-start
|
|
6
|
+
|
|
7
|
+
> **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host).
|
|
8
|
+
|
|
9
|
+
## On this page
|
|
10
|
+
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Identifying users](#identifying-users)
|
|
13
|
+
- [Initialize PostHog on the client](#initialize-posthog-on-the-client)
|
|
14
|
+
- [Capture events on the client](#capture-events-on-the-client)
|
|
15
|
+
- [Initialize PostHog on the server](#initialize-posthog-on-the-server)
|
|
16
|
+
- [Capture events on the server](#capture-events-on-the-server)
|
|
17
|
+
- [Next steps](#next-steps)
|
|
18
|
+
|
|
5
19
|
This tutorial shows how to integrate PostHog with a [TanStack Start](https://tanstack.com/start) app for both client-side and server-side analytics.
|
|
6
20
|
|
|
7
21
|
## Installation
|
|
8
22
|
|
|
9
23
|
Install the required packages:
|
|
10
24
|
|
|
11
|
-
Terminal
|
|
12
|
-
|
|
13
25
|
```bash
|
|
14
26
|
npm install @posthog/react posthog-node
|
|
15
27
|
```
|
|
16
28
|
|
|
17
|
-
- `@posthog/react` - React package for our [JS Web SDK](/docs/libraries/js.md) for client-side usage
|
|
18
|
-
- `posthog-node` - PostHog [Node.js SDK](/docs/libraries/node.md) for server-side event capture
|
|
29
|
+
- `@posthog/react` - React package for our [JS Web SDK](https://posthog.com/docs/libraries/js.md) for client-side usage
|
|
30
|
+
- `posthog-node` - PostHog [Node.js SDK](https://posthog.com/docs/libraries/node.md) for server-side event capture
|
|
19
31
|
|
|
20
32
|
## Identifying users
|
|
21
33
|
|
|
22
|
-
> **Identifying users is required.** Call `posthog.identify('your-user-id')` after login to link events to a known user. This is what connects frontend event captures, [session replays](/docs/session-replay.md), [LLM traces](/docs/ai-engineering.md), and [error tracking](/docs/error-tracking.md) to the same person — and lets backend events link back too.
|
|
34
|
+
> **Identifying users is required.** Call `posthog.identify('your-user-id')` after login to link events to a known user. This is what connects frontend event captures, [session replays](https://posthog.com/docs/session-replay.md), [LLM traces](https://posthog.com/docs/ai-engineering.md), and [error tracking](https://posthog.com/docs/error-tracking.md) to the same person — and lets backend events link back too.
|
|
23
35
|
>
|
|
24
36
|
> Use a stable ID from your auth system when possible, not an email or display name. Send those as person properties instead. If your app has no other stable key, email works as a fallback if they are unique. Never a shared literal like `"anonymous"` or `"user"`, which pools many people onto one person and corrupts their data. When no ID is available at all, skip the identify and retain the anonymous distinct ID that's automatically assigned.
|
|
25
37
|
>
|
|
26
38
|
> Call `posthog.reset()` on logout, so the next person to use the browser doesn't inherit the last one's identity.
|
|
27
39
|
>
|
|
28
|
-
> See our guide on [identifying users](/docs/getting-started/identify-users.md) for how to set this up.
|
|
40
|
+
> See our guide on [identifying users](https://posthog.com/docs/getting-started/identify-users.md) for how to set this up.
|
|
29
41
|
|
|
30
42
|
If your app calls your own backend, `tracing_headers` adds `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` to matching `fetch` and `XMLHttpRequest` requests. This lets server-side SDKs link backend events, errors, and LLM traces back to frontend sessions and replays. Use hostnames only, without protocols or paths.
|
|
31
43
|
|
|
32
|
-
JavaScript
|
|
33
|
-
|
|
34
44
|
```javascript
|
|
35
45
|
posthog.init('<ph_project_token>', {
|
|
36
46
|
api_host: 'https://us.i.posthog.com',
|
|
@@ -118,8 +128,6 @@ function CheckoutButton({ orderId, total }: { orderId: string; total: number })
|
|
|
118
128
|
|
|
119
129
|
Call `posthog.identify()` when a user logs in to link their events to a user ID:
|
|
120
130
|
|
|
121
|
-
TSX
|
|
122
|
-
|
|
123
131
|
```jsx
|
|
124
132
|
import { usePostHog } from '@posthog/react'
|
|
125
133
|
|
|
@@ -208,12 +216,4 @@ The server-side `capture` call requires a `distinctId` (the user identifier), an
|
|
|
208
216
|
|
|
209
217
|
## Next steps
|
|
210
218
|
|
|
211
|
-
Installing the JS Web SDK and Node SDK means all of their functionality is available in your TanStack Start project. To learn more about this, have a look at our [JS Web SDK docs](/docs/libraries/js/usage.md) and [Node SDK docs](/docs/libraries/node.md).
|
|
212
|
-
|
|
213
|
-
### Still have questions?
|
|
214
|
-
|
|
215
|
-
Ask PostHog AI
|
|
216
|
-
|
|
217
|
-
### Was this page useful?
|
|
218
|
-
|
|
219
|
-
HelpfulCould be better
|
|
219
|
+
Installing the JS Web SDK and Node SDK means all of their functionality is available in your TanStack Start project. To learn more about this, have a look at our [JS Web SDK docs](https://posthog.com/docs/libraries/js/usage.md) and [Node SDK docs](https://posthog.com/docs/libraries/node.md).
|