From fd98f31e2c1ca1ead2ee16faf3cd5ea84d61560f Mon Sep 17 00:00:00 2001 From: joeynmq <37472597+joeynmq@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:40:33 +0800 Subject: [PATCH] Update sample code for Node.js Typewritter --- .../apis-and-extensions/typewriter.md | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/protocols/apis-and-extensions/typewriter.md b/src/protocols/apis-and-extensions/typewriter.md index 130d61c445..b42d9f6d4c 100644 --- a/src/protocols/apis-and-extensions/typewriter.md +++ b/src/protocols/apis-and-extensions/typewriter.md @@ -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: '' }) - // 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 ""