Skip to content

Update sample code for Node.js Typewritter #5538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/protocols/apis-and-extensions/typewriter.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,27 @@ To get started with Node.js:
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:

```ts
// Import your auto-generated Typewriter client.
import typewriter from './analytics'

// Initialize analytics-node, per the analytics-node guide above.
import { Analytics } from '@segment/analytics-node'

export const analytics = new Analytics({ writeKey: 'YOUR_WRITE_KEY' })

// Pass in your analytics-node instance to Typewriter.
typewriter.setTypewriterOptions({
analytics: analytics
})
const analytics = new Analytics({ writeKey: '<MY_WRITE_KEY>' })

// Issue your first Typewriter track call!
typewriter.orderCompleted({
orderID: 'ck-f306fe0e-cc21-445a-9caa-08245a9aa52c',
total: 39.99
app.post('/login', (req, res) => {
analytics.identify({
userId: req.body.userId,
previousId: req.body.previousId
})
res.sendStatus(200)
})

app.post('/cart', (req, res) => {
analytics.track({
userId: req.body.userId,
event: 'Add to cart',
properties: { productId: '123456' }
})
res.sendStatus(201)
});
```

> info ""
Expand Down