Skip to content

Commit 5fa13b2

Browse files
formatting updates (#179)
## 💸 TL;DR <!-- What's the three sentence summary of purpose of the PR --> Improve code block formatting and syntax highlighting. Does not change the actual example code behavior.
1 parent 62a6166 commit 5fa13b2

10 files changed

Lines changed: 211 additions & 140 deletions

File tree

docs/capabilities/analytics/devvit-journeys.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
14
# Devvit Journeys
25

36
Devvit Journeys adds a telemetry stream to your app that tracks the entire lifecycle of a user session. With journeys, you can:
@@ -85,7 +88,7 @@ By instrumenting these moments, you can track session boundaries, understand pla
8588
| :---: | ------------------------------------- | ----------------------- | ----------------------------- |
8689
| **1** | Game loads | **App.Ready** | Game is fully interactive |
8790
| **2** | Player clicks “Start Game” | **Journey.Start** | Begins a new session |
88-
| **3** | Player completes Level 1(of 5 levels) | **Journey.Progress** | `progress: 0.2` |
91+
| **3** | Player completes Level 1 (of 5 levels) | **Journey.Progress** | `progress: 0.2` |
8992
| **4** | Player opens inventory | **Journey.Interaction** | `action: "menu_opened"` |
9093
| **5** | Player completes Level 2 | **Journey.Progress** | `progress: 0.4` |
9194
| **6** | Player reaches final level | **Journey.Progress** | `progress: 0.9` |
@@ -98,7 +101,7 @@ By instrumenting these moments, you can track session boundaries, understand pla
98101
| **1** | Game loads | **App.Ready** | Game is fully interactive |
99102
| **2** | Player clicks “Start Game” | **Journey.Start** | Begins a new session |
100103
| **3** | Player completes early level | **Journey.Progress** | `progress: 0.3` |
101-
| **4** | Player dies | **Journey.End** | `complete: false`, `win: false` |
104+
| **4** | Player dies | **Journey.End** | `complete: false`, `win: false` |
102105

103106
### Scenario 3: early exit / abandonment
104107

@@ -149,7 +152,7 @@ Here's how to implement journey tracking in your app.
149152

150153
Set Journeys permissions to `true` in `devvit.json`.
151154

152-
```
155+
```json title="devvit.json"
153156
"permissions": {
154157
"journeys": true
155158
},
@@ -159,7 +162,7 @@ Set Journeys permissions to `true` in `devvit.json`.
159162

160163
You can send events solely on the backend and use the front‑end only to establish and pass along the journeyId. To do this, thread the active `journey ID` from your front‑end to your backend routes.
161164

162-
```
165+
```ts title="client/index.ts"
163166
import { telemetry } from '@devvit/analytics/client/reddit';
164167

165168
export async function submitScore(score: number): Promise<void> {
@@ -184,7 +187,50 @@ export async function submitScore(score: number): Promise<void> {
184187

185188
On the server, read the incoming `journeyId` and use it for correlation in your own route.
186189

190+
<Tabs
191+
variant="pill"
192+
groupId="http-server-framework"
193+
defaultValue="hono"
194+
values={[
195+
{ label: 'Hono', value: 'hono' },
196+
{ label: 'Express', value: 'express' },
197+
]}>
198+
<TabItem value="hono">
199+
200+
```ts title="server/index.ts"
201+
import { telemetry } from '@devvit/analytics/server/reddit';
202+
import { Hono } from 'hono';
203+
204+
const app = new Hono();
205+
206+
app.post('/api/score', async (c) => {
207+
const journeyId = c.req.header('x-devvit-journey-id') ?? '';
208+
const { score } = await c.req.json<{ score: number }>();
209+
210+
console.log('score event', {
211+
journeyId,
212+
score,
213+
});
214+
215+
await telemetry.endJourney({
216+
journeyId,
217+
complete: true,
218+
game: {
219+
win: true,
220+
score,
221+
},
222+
});
223+
224+
return c.json({ ok: true });
225+
});
226+
227+
export default app;
187228
```
229+
230+
</TabItem>
231+
<TabItem value="express">
232+
233+
```ts title="server/index.ts"
188234
import express from 'express';
189235
import { telemetry } from '@devvit/analytics/server/reddit';
190236

@@ -209,16 +255,18 @@ app.post('/api/score', async (req, res) => {
209255

210256
res.json({ ok: true });
211257
});
212-
213258
```
214259

260+
</TabItem>
261+
</Tabs>
262+
215263
### Client events
216264

217265
If you don’t want to manually send server-events, you can use the generic client side events. In this case, the `JourneyId` is handled. In this case, you won’t need to pass a `JourneyId` when calling progress and so forth. You also won’t need `telemetry.getActiveJourneyId()` unless you’re curious about that data.
218266

219267
Note: This also requires using the route adapters provided in `@devvit/analytics/server/reddit`
220268

221-
```
269+
```ts title="client/index.ts"
222270
// client
223271
import { telemetry } from '@devvit/analytics/client/reddit';
224272

@@ -231,7 +279,7 @@ await telemetry.progress({
231279

232280
```
233281

234-
```
282+
```ts title="server/index.ts"
235283
// server
236284
import { createTelemetryRouter } from '@devvit/analytics/server/reddit';
237285
app.use(createTelemetryRouter());

docs/capabilities/http-fetch.md

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ Your Devvit app can make network requests to access allow-listed external domain
66

77
## Enabling HTTP fetch calls
88

9-
devvit.json
10-
11-
```
12-
9+
```json title="devvit.json"
1310
{
14-
...
15-
"permissions": {
16-
"http": {
17-
"enable": true,
18-
"domains": ["my-site.com", "another-domain.net"]
19-
}
20-
}
11+
...
12+
"permissions": {
13+
"http": {
14+
"enable": true,
15+
"domains": ["my-site.com", "another-domain.net"]
16+
}
17+
}
2118
}
2219
```
2320

@@ -56,15 +53,12 @@ Devvit Web applications have two different contexts for using fetch:
5653

5754
Server-side fetch allows your app to make HTTP requests to allowlisted external domains from your server-side code (e.g., API routes, server actions):
5855

59-
server/index.ts
60-
61-
```
62-
56+
```ts title="server/index.ts"
6357
const response = await fetch('https://example.com/api/data', {
64-
method: 'GET',
65-
headers: {
66-
'Content-Type': 'application/json',
67-
},
58+
method: 'GET',
59+
headers: {
60+
'Content-Type': 'application/json',
61+
},
6862
});
6963

7064
const data = await response.json();
@@ -78,22 +72,20 @@ Client-side fetch has different restrictions:
7872
- **Domain limitation**: Can only make requests to your own webview domain
7973
- **Endpoint requirement**: All requests must target endpoints that start with /api/
8074
- **Authentication**: Handled automatically \- no need to manage auth tokens
81-
- **No external domains**: Cannot make requests to external domains from client-side code
82-
client/index.ts
83-
84-
```
75+
- **No external domains**: Cannot make requests to external domains from client-side code
8576

77+
```ts title="client/index.ts"
8678
const handleFetchData = async () => {
87-
// ✅ Correct: Fetching your own webview's API endpoint
88-
const response = await fetch("/api/user-data", {
89-
method: "GET",
90-
headers: {
91-
"Content-Type": "application/json",
92-
},
93-
});
94-
95-
const data = await response.json();
96-
console.log("API response:", data);
79+
// ✅ Correct: Fetching your own webview's API endpoint
80+
const response = await fetch("/api/user-data", {
81+
method: "GET",
82+
headers: {
83+
"Content-Type": "application/json",
84+
},
85+
});
86+
87+
const data = await response.json();
88+
console.log("API response:", data);
9789
};
9890

9991
// ❌ Incorrect: Cannot fetch external domains from client-side
@@ -107,7 +99,7 @@ const handleFetchData = async () => {
10799

108100
The following error means HTTP Fetch requests are hitting the internal timeout limits.
109101

110-
```
102+
```text
111103
HTTP request to domain: <domain> timed out with error: context deadline exceeded.
112104
```
113105

@@ -191,8 +183,7 @@ If your app uses fetch domains, add this context to your app's [README](../devvi
191183

192184
Example Fetch Domains section:
193185

194-
```
195-
186+
```md title="README.md"
196187
## Fetch Domains
197188

198189
The following domains are requested for this app:

docs/capabilities/notifications/pn-best-practices.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ To learn more about creating deeper engagement loops, check out the best practic
127127

128128
In your terminal, navigate to your project directory and run this command to update the push notification to the latest release.
129129

130-
```
130+
```bash
131131
npm install @devvit/notifications
132132
```
133133

134134
### Step 2: Import the push notification module
135135

136-
```
136+
```ts
137137
import { notifications } from '@devvit/notifications';
138138
```
139139

@@ -144,7 +144,7 @@ import { notifications } from '@devvit/notifications';
144144

145145
To send a push notification to a group of users, you can use the double curly brackets ( { { } } ) to reference variables in a Mustache template.
146146

147-
```
147+
```ts
148148
await notifications.enqueue({
149149
title: 'Hello {{name}}!',
150150
body: 'You have {{score}} new points.',
@@ -179,7 +179,7 @@ await notifications.enqueue({
179179

180180
**Note:** Mustache templating is optional. Here's a simplified example without it:
181181

182-
```
182+
```ts
183183
await notifications.enqueue({
184184
title: 'Winner!',
185185
body: 'Congrats on your win',
@@ -189,6 +189,7 @@ await notifications.enqueue({
189189
link: 't3_xyz987',
190190
},
191191
],
192+
});
192193
```
193194

194195
**Note**: If the app hasn’t been published, you can only send push notifications to yourself for testing. **Pre-release apps in testing are not subject to the rate-limits below**.
@@ -204,14 +205,14 @@ If you need higher limits, let us know.
204205

205206
Users will be able to opt in or out of receiving notifications triggered by a button in your UI:
206207

207-
```
208+
```ts
208209
await notifications.optInCurrentUser();
209210
await notifications.optOutCurrentUser();
210211
```
211212

212213
You will also be able to retrieve a list of users who have opted in (if not managing it manually):
213214

214-
```
215+
```ts
215216
//This will just return the first 1000 users
216217
const recipients = await notifications.listOptedInUsers();
217218

docs/capabilities/server/http-fetch.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const response = await fetch('https://example.com/api/data', {
6161

6262
const data = await response.json();
6363
console.log('External API response:', data);
64-
````
64+
```
6565

6666
### Client-side fetch
6767

@@ -106,7 +106,7 @@ If you see the following error, it means HTTP Fetch requests are hitting the int
106106
- Use a queue or kick off an async request in your back end. You can use [Scheduler](./scheduler.mdx) to monitor the result.
107107
- Optimize the overall HTTP request latency if you have a self-hosted server.
108108

109-
```ts
109+
```text
110110
HTTP request to domain: <domain> timed out with error: context deadline exceeded.
111111
```
112112

0 commit comments

Comments
 (0)