Skip to content

Commit 2c980fe

Browse files
Merge pull request #5538 from segmentio/joeynmq-patch-5
Update sample code for Node.js Typewritter
2 parents 5b521cc + fd98f31 commit 2c980fe

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/protocols/apis-and-extensions/typewriter.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,27 @@ To get started with Node.js:
162162
4. Run `npx typewriter init` to use the Typewriter quickstart wizard that generates a [`typewriter.yml`](#configuration-reference) configuration, along with your first Typewriter client. When you run the command, it creates a `typewriter.yml` file in your repo. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference). The command also adds a new Typewriter client in `./analytics` (or whichever path you configured). You can import this client into your project, like so:
163163
164164
```ts
165-
// Import your auto-generated Typewriter client.
166-
import typewriter from './analytics'
167-
168165
// Initialize analytics-node, per the analytics-node guide above.
169166
import { Analytics } from '@segment/analytics-node'
170167
171-
export const analytics = new Analytics({ writeKey: 'YOUR_WRITE_KEY' })
172-
173-
// Pass in your analytics-node instance to Typewriter.
174-
typewriter.setTypewriterOptions({
175-
analytics: analytics
176-
})
168+
const analytics = new Analytics({ writeKey: '<MY_WRITE_KEY>' })
177169
178-
// Issue your first Typewriter track call!
179-
typewriter.orderCompleted({
180-
orderID: 'ck-f306fe0e-cc21-445a-9caa-08245a9aa52c',
181-
total: 39.99
170+
app.post('/login', (req, res) => {
171+
analytics.identify({
172+
userId: req.body.userId,
173+
previousId: req.body.previousId
174+
})
175+
res.sendStatus(200)
182176
})
177+
178+
app.post('/cart', (req, res) => {
179+
analytics.track({
180+
userId: req.body.userId,
181+
event: 'Add to cart',
182+
properties: { productId: '123456' }
183+
})
184+
res.sendStatus(201)
185+
});
183186
```
184187
185188
> info ""

0 commit comments

Comments
 (0)