Skip to content

Commit 3e20a5b

Browse files
authored
Merge pull request #4664 from segmentio/update-typewriter
2 parents 23bc4da + 465cc2c commit 3e20a5b

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

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

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ At a high-level, Typewriter can take an event from your Tracking Plan like this
1111

1212
Typewriter uses the event to generate a typed analytics call in different languages:
1313

14-
```js
14+
```ts
1515
// Example client in your web app
16-
const typewriter = require('./analytics')
16+
import typewriter from './analytics'
1717

1818
typewriter.orderCompleted({
1919
orderID: 'ck-f306fe0e-cc21-445a-9caa-08245a9aa52c',
@@ -63,19 +63,13 @@ Have feedback on Typewriter? Consider opening a [GitHub issue here](https://gith
6363

6464
## Prerequisites
6565

66-
Typewriter is built using [Node.js](https://nodejs.org/en/), and requires `node@8.x` or later, and `[email protected]` or later to function.
66+
Typewriter is built using [Node.js](https://nodejs.org/en/), and requires node >= 14.x
6767

6868
You can check if you have Node and NPM installed by running the following commands in your command-line window:
6969

7070
```sh
7171
$ node --version
72-
v10.15.3
73-
74-
$ npm --version
75-
6.9.0
76-
77-
$ npx --version
78-
6.9.0
72+
v14.x
7973
```
8074

8175
If you don't have these, [you'll need to install `node`](https://nodejs.org/en/download/package-manager). Installing `node` also installs `npm` and `npx` for you. If you're on macOS, you can install it with [Homebrew](https://brew.sh/):
@@ -90,38 +84,62 @@ Once you've installed Node and NPM, run the `--version` commands again to verify
9084

9185
To get started with Typewriter in your browser:
9286
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites) above.
93-
2. Install `analytics.js` in your app. For now, you just need to complete [`Step 1: Copy the Snippet`](/docs/connections/sources/catalog/libraries/website/javascript/quickstart/#step-2-copy-the-segment-snippet) from the [`analytics.js` Quickstart Guide](/docs/connections/sources/catalog/libraries/website/javascript/quickstart/).
87+
2. Install `analytics.js` in your app. There are two methods.
88+
- **Snippet method (most common)**: Paste the snippet in the[`Step 1: Copy the Snippet`](/docs/connections/sources/catalog/libraries/website/javascript/quickstart/#step-2-copy-the-segment-snippet) from the [`analytics.js` Quickstart Guide](/docs/connections/sources/catalog/libraries/website/javascript/quickstart/).
89+
- **NPM method**: Load analytics.js with the npm library. Learn more about using the npm method [here](https://github.com/segmentio/analytics-next/tree/master/packages/browser#readme).
90+
9491
3. Once you've got `analytics.js` installed, add Typewriter as a developer dependency in your project:
9592

9693
```sh
9794
$ npm install --save-dev typewriter
9895
```
99-
100-
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 project. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference).
101-
96+
4. If you are a snippet user that uses TypeScript, you should also install the npm library as a dev dependency to get the typescript types.
97+
```sh
98+
$ npm install --save-dev @segment/analytics-next
99+
```
100+
5. 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 project. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference).
102101
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:
103102

104-
```js
103+
```ts
105104
// Import your auto-generated Typewriter client:
106-
const typewriter = require('./analytics')
105+
import typewriter from './analytics'
107106
108107
// Issue your first Typewriter track call!
109108
typewriter.orderCompleted({
110109
orderID: 'ck-f306fe0e-cc21-445a-9caa-08245a9aa52c',
111110
total: 39.99
112111
})
113112
```
113+
### Configuration for snippet + TypeScript users
114+
```ts
115+
// Optional but recommended: you can improve your developer experience by adding typings for the global analytics object.
116+
import type { AnalyticsSnippet } from '@segment/analytics-next'
117+
118+
declare global {
119+
interface Window {
120+
analytics: AnalyticsSnippet;
121+
}
122+
```
123+
### Configuration for NPM users
124+
```ts
125+
// As an npm user, you *must* explicitly pass in your analytics instance.
126+
import { AnalyticsBrowser } from '@segment/analytics-next'
127+
128+
const analytics = AnalyticsBrowser.load({ writeKey: 'YOUR_WRITE_KEY' })
129+
130+
typewriter.setTypewriterOptions({
131+
analytics: analytics
132+
})
133+
```
114134
115135
> info ""
116136
> Run `npx typewriter` to regenerate your Typewriter client. You need to do this each time you update your Tracking Plan.
117137
118138
To help you minimize your bundle size, Typewriter supports [tree-shaking](https://webpack.js.org/guides/tree-shaking/){:target="_blank"} using named exports. All generated analytics calls generate and export automatically, so you can import them like so:
119139
120-
```js
121-
// Import your auto-generated Typewriter client:
122-
const { orderCompleted } = require('./analytics')
140+
```ts
141+
import { orderCompleted } from './analytics'
123142
124-
// Issue your first Typewriter track call!
125143
orderCompleted({
126144
orderID: 'ck-f306fe0e-cc21-445a-9caa-08245a9aa52c',
127145
total: 39.99
@@ -132,9 +150,9 @@ Typewriter wraps your analytics calls in an [ES6 `Proxy`](https://developer.mozi
132150
133151
## Node.js Quickstart
134152
135-
To get started with Nodejs:
153+
To get started with Node.js:
136154
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites) above.
137-
2. Install `analytics-node` in your app. For now, you just need to complete [`Step 2: Install the Module`](/docs/connections/sources/catalog/libraries/server/node/quickstart/#step-2-install-the-module) from the [`analytics-node` Quickstart Guide](/docs/connections/sources/catalog/libraries/server/node/quickstart).
155+
2. Install `@segment/analytics-node` in your app. For now, you just need to complete [`Step 2: Install the Module`](/docs/connections/sources/catalog/libraries/server/node/quickstart/#step-2-install-the-module) from the [`analytics-node` Quickstart Guide](/docs/connections/sources/catalog/libraries/server/node/quickstart).
138156
3. Once you have `analytics-node` installed, add Typewriter as a developer dependency in your project:
139157
140158
```sh
@@ -143,13 +161,14 @@ To get started with Nodejs:
143161
144162
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:
145163
146-
```js
164+
```ts
165+
// Import your auto-generated Typewriter client.
166+
import typewriter from './analytics'
167+
147168
// Initialize analytics-node, per the analytics-node guide above.
148-
const Analytics = require('analytics-node')
149-
const analytics = new Analytics('YOUR_WRITE_KEY')
169+
import { Analytics } from '@segment/analytics-node'
150170
151-
// Import your auto-generated Typewriter client.
152-
const typewriter = require('./analytics')
171+
export const analytics = new Analytics({ writeKey: 'YOUR_WRITE_KEY' })
153172
154173
// Pass in your analytics-node instance to Typewriter.
155174
typewriter.setTypewriterOptions({
@@ -330,7 +349,7 @@ Segment recommends you use a [Token Script](#token-script) to share an API token
330349
Segment also recommends you to pipe through your API Token as this will let you keep your token secret, but it also allows you to share it across your team.
331350
332351
> warning ""
333-
> Segment is keeping the Token Script execution for compatibility purposes only in v8 of Typewriter. Segment might deprecate this feature in the future, and encourages you to execute your script and pipe in the token. For example, `echo $TW_TOKEN | typewriter build`.
352+
> Segment is temporarily keeping the Token Script execution for compatibility purposes. Segment might deprecate this feature in the future, and encourages you to execute your script and pipe in the token. For example, `echo $TW_TOKEN | typewriter build`.
334353
335354
## Editor Configuration
336355
@@ -490,10 +509,10 @@ You can provide a custom handler that fires whenever a violation is seen. By def
490509
491510
For `analytics.js` and `analytics-node` clients, you can configure this handler with `setTypewriterOptions`:
492511
493-
```js
494-
const typewriter = require('./analytics')
512+
```ts
513+
import typewriter from './analytics'
495514
496-
function yourViolationHandler(message, violations) {
515+
const yourViolationHandler = (message, violations) => {
497516
console.error(`Typewriter Violation found in ${message.event}`, violations)
498517
}
499518
@@ -504,10 +523,10 @@ typewriter.setTypewriterOptions({
504523
505524
A common use case for this handler is to configure Typewriter to detect when your tests are running and if so, throw an error to fail your unit tests. For example:
506525
507-
```js
526+
```ts
508527
const typewriter = require('./analytics')
509528
510-
function yourViolationHandler(message, violations) {
529+
const yourViolationHandler = (message, violations) => {
511530
if (process.env.IS_TESTING === 'true') {
512531
throw new Error(`Typewriter Violation found in ${message.event}`)
513532
}
@@ -524,9 +543,9 @@ Another common use case is to customize how violations are reported to your team
524543
525544
![Example toaster notification on app.segment.com](images/typewriter-violation-toast.png)
526545
527-
```js
528-
const typewriter = require('./analytics')
529-
const { toaster } = require('evergreen-ui')
546+
```ts
547+
import typewriter from './analytics'
548+
import { toaster } from 'evergreen-ui'
530549
531550
typewriter.setTypewriterOptions({
532551
// Note that this handler only fires in development mode, since we ship the production build

0 commit comments

Comments
 (0)