Skip to content

Commit ab6c256

Browse files
Merge pull request #7916 from segmentio/DOC-1192
Typewriter review
2 parents 4cf1bd7 + 2a603fa commit ab6c256

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

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

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ redirect_from: '/protocols/typewriter/'
44
---
55

66
> warning ""
7-
> Typewriter for analytics.js and analytics-node will receive no new features and only critical maintenance updates from Segment. Typewriter for other libraries and SDKs are not actively maintained by Segment. Typewriter is available on [GitHub](https://github.com/segmentio/typewriter/){:target="_blank”} under the MIT license for the open-source community to fork and contribute.
7+
> Typewriter for Analytics.js and Analytics-Node will receive no new features and only critical maintenance updates from Segment. Typewriter for other libraries and SDKs are not actively maintained by Segment. Typewriter is available on [GitHub](https://github.com/segmentio/typewriter/){:target="_blank”} under the MIT license for the open-source community to fork and contribute.
88
99
[Typewriter](https://github.com/segmentio/typewriter){:target="_blank”} is a tool for generating strongly-typed Segment analytics libraries based on your pre-defined [Tracking Plan](/docs/protocols/tracking-plan) spec.
1010

1111
At a high-level, Typewriter can take an event from your Tracking Plan like this `"Order Completed"` event:
1212

1313
![Order Completed Event in a Protocols Tracking Plan](images/typewriter-order-completed.png)
1414

15-
Typewriter uses the event to generate a typed analytics call in different languages:
15+
Typewriter uses the event to generate a typed Analytics call in different languages:
1616

1717
```ts
1818
// Example client in your web app
@@ -34,9 +34,9 @@ SEGTypewriterAnalytics.orderCompleted(
3434
> info ""
3535
> Typewriter can generate clients for `analytics.js`, `analytics-node`, `analytics-swift` and `analytics-kotlin`.
3636
37-
These generated clients are embedded with metadata from your Tracking Plan, which contextualizes your analytics instrumentation, and reduces (or entirely eliminates!) incorrect instrumentations in your production environments. In your editor, you can access event names, descriptions, property names, types and more:
37+
These generated clients are embedded with metadata from your Tracking Plan, which contextualizes your analytics instrumentation, and reduces (or entirely eliminates) incorrect instrumentations in your production environments. In your editor, you can access event names, descriptions, property names, types and more:
3838

39-
![Event name intellisense](images/typewriter-event-names.png)
39+
![Event name intelli-sense](images/typewriter-event-names.png)
4040

4141
You can also configure Typewriter to validate analytic events at runtime, which can alert you to instrumentation errors during development and testing. Typewriter can warn you about missing required properties, invalid enum values, regex mismatches, and any other advanced [JSON Schema](https://json-schema.org/understanding-json-schema/) you configure in your Tracking Plan.
4242

@@ -46,11 +46,11 @@ You can use this with a test suite to automatically fail your unit tests if the
4646

4747
![Example unit tests failing because of violations](images/typewriter-test-suite.png)
4848

49-
If you use a statically typed language (such as TypeScript, Java, Objective-C, or Swift), you get access to compile-time warnings about your instrumentation:
49+
If you use a statically typed language (like TypeScript, Java, Objective-C, or Swift), you get access to compile-time warnings about your instrumentation:
5050

5151
![Example compile-time validation warnings](images/typewriter-compile-time-warnings.png)
5252

53-
Typewriter also helps teams adopt [analytics best practices](/docs/protocols/tracking-plan/best-practices/), such as avoiding autogenerated event names, and carefully considering what properties are tracked.
53+
Typewriter also helps teams adopt [Analytics best practices](/docs/protocols/tracking-plan/best-practices/), like avoiding autogenerated event names and carefully considering what properties are tracked.
5454

5555
To get started, check out one of the quickstart guides below:
5656
- [Browser Quickstart](#browser-quickstart)
@@ -66,7 +66,7 @@ Have feedback on Typewriter? Consider opening a [GitHub Issue in the @segmentio/
6666

6767
## Prerequisites
6868

69-
Typewriter is built using [Node.js](https://nodejs.org/en/){:target="_blank”}, and requires node >= 14.x
69+
Typewriter is built using [Node.js](https://nodejs.org/en/){:target="_blank”}, and requires node >= 14.x.
7070

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

@@ -83,11 +83,11 @@ $ brew install node
8383

8484
Once you've installed Node and NPM, run the `--version` commands again to verify that they were installed correctly.
8585

86-
## Browser Quickstart
86+
## Browser quickstart
8787

8888
To get started with Typewriter in your browser:
89-
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites) above.
90-
2. Install `analytics.js` in your app. There are two methods.
89+
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites).
90+
2. Install `analytics.js` in your app using one of the following methods:
9191
- **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/).
9292
- **NPM method**: Load analytics.js with the npm library. Learn more about using the npm method in the [@segmentio/analytics-next](https://github.com/segmentio/analytics-next/tree/master/packages/browser#readme){:target="_blank”} repository.
9393

@@ -138,7 +138,7 @@ To get started with Typewriter in your browser:
138138
> info ""
139139
> Run `npx typewriter` to regenerate your Typewriter client. You need to do this each time you update your Tracking Plan.
140140
141-
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:
141+
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 this:
142142
143143
```ts
144144
import { orderCompleted } from './analytics'
@@ -149,20 +149,20 @@ orderCompleted({
149149
})
150150
```
151151
152-
Typewriter wraps your analytics calls in an [ES6 `Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy){:target="_blank"}, which helps protect your application from crashing if you make analytics calls with a generated function that doesn't exist. For example, if an `Order Completed` event didn't exist in your Tracking Plan in the first example above, then your app would crash with a `TypeError: typewriter.orderCompleted is not a function`. However, since Typewriter dynamically proxies the underlying function calls, it can detect if a function doesn't exist, and handle it for you. Typewriter logs a warning message, then fires an `Unknown Analytics Call Fired` event into your source. This helps to prevent regressions when you migrate JavaScript projects to Typewriter in bulk. Keep in mind that proxying doesn't work with named exports.
152+
Typewriter wraps your analytics calls in an [ES6 `Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy){:target="_blank"}, which helps protect your application from crashing if you make analytics calls with a generated function that doesn't exist. For example, if an `Order Completed` event didn't exist in your Tracking Plan in the first example above, then your app would crash with a `TypeError: typewriter.orderCompleted is not a function` error. However, since Typewriter dynamically proxies the underlying function calls, it can detect if a function doesn't exist, and handle it for you. Typewriter logs a warning message, then fires an `Unknown Analytics Call Fired` event into your source. This helps to prevent regressions when you migrate JavaScript projects to Typewriter in bulk. Keep in mind that proxying doesn't work with named exports.
153153
154-
## Node.js Quickstart
154+
## Node.js quickstart
155155
156156
To get started with Node.js:
157-
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites) above.
157+
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites).
158158
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).
159159
3. Once you have `analytics-node` installed, add Typewriter as a developer dependency in your project:
160160
161161
```sh
162162
$ npm install --save-dev typewriter
163163
```
164164
165-
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:
165+
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 repository. 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:
166166
167167
```ts
168168
// Initialize analytics-node, per the analytics-node guide above.
@@ -193,15 +193,15 @@ To get started with Node.js:
193193
194194
Typewriter wraps your analytics calls in an [ES6 `Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy){:target="_blank”}, which helps protect your application from crashing if you make analytics calls with a generated function that doesn't exist. For example, if an `Order Completed` event didn't exist in your Tracking Plan in the first example above, then your app would crash with a `TypeError: typewriter.orderCompleted is not a function`. However, since `typewriter` dynamically proxies the underlying function calls, it can detect if a function does not exist, and handle it for you. Typewriter logs a warning message, then fires an `Unknown Analytics Call Fired` event into your source. This helps to prevent regressions when you migrate JavaScript projects to Typewriter in bulk. Keep in mind that proxying doesn't work with named exports.
195195
196-
## Swift Quickstart
196+
## Swift quickstart
197197
198198
> info ""
199199
> For use with the `analytics-ios` SDK, use [Typewriter v7](/docs/protocols/apis-and-extensions/typewriter-v7).
200200
201201
To get started using Typewriter with Swift:
202-
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites) above.
202+
1. Make sure you have `node` installed using the instructions in the [prerequisites](#prerequisites).
203203
2. Install `analytics-swift` in your app. Follow the [analytics-swift Quickstart Guide](/docs/connections/sources/catalog/libraries/mobile/swift).
204-
3. 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).
204+
3. 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 repository. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference).
205205
206206
<br> **Note:** Run `npx typewriter` to regenerate your Typewriter client. You need to do this each time you update your Tracking Plan.
207207
@@ -216,15 +216,15 @@ To get started using Typewriter with Swift:
216216
))
217217
```
218218
219-
## Kotlin Quickstart
219+
## Kotlin quickstart
220220
221221
> info ""
222222
> For use with the `analytics-android` SDK, use [Typewriter v7](/docs/protocols/apis-and-extensions/typewriter-v7).
223223
224224
To get started using Typewriter with Kotlin:
225-
1. Make sure you have `node` installed. Use the instructions in the [prerequisites](#prerequisites) above.
225+
1. Make sure you have `node` installed. Use the instructions in the [prerequisites](#prerequisites).
226226
2. Install `analytics-kotlin` in your app. Follow the [analytics-kotlin QuickStart Guide](/docs/connections/sources/catalog/libraries/mobile/kotlin-android/#getting-started).
227-
3. Run `npx typewriter init`. This command enables you to use the Typewriter quickstart wizard that generates a [`typewriter.yml`](#configuration-reference) configuration, along with your first Typewriter client. The command creates a `typewriter.yml` file in your repo. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference).
227+
3. Run `npx typewriter init`. This command enables you to use the Typewriter quickstart wizard that generates a [`typewriter.yml`](#configuration-reference) configuration, along with your first Typewriter client. The command creates a `typewriter.yml` file in your repository. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference).
228228
229229
Typewriter creates the class file with the package name `typewriter`. Segment recommends you to enter the right package name during `npx typewriter init` by choosing to review the Advanced Options for Kotlin. You can also enter the right package name directly in `typewriter.yml`:
230230
@@ -234,19 +234,21 @@ To get started using Typewriter with Kotlin:
234234
sdk: kotlin
235235
languageOptions:
236236
package: com.segment.typewriter
237-
```
237+
```
238238
239239
> info ""
240240
> Run `npx typewriter` to regenerate your Typewriter client. You need to do this each time you update your Tracking Plan.
241241
242242
You can now use your Typewriter client in your Android Kotlin or Java application as extensions to any `Analytics` object:
243243
244244
Kotlin:
245+
245246
```kotlin
246247
// Import your auto-generated Typewriter client:
247248
import com.segment.generated.*
248249
analytics.orderCompleted(OrderCompleted(orderID = "110", total = 39.98))
249250
```
251+
250252
Java:
251253
```java
252254
// Import your auto-generated Typewriter client:
@@ -261,7 +263,7 @@ TypewriterAnalytics.with(this).orderCompleted(
261263
);
262264
```
263265
264-
## React Native Quickstart
266+
## React Native quickstart
265267
266268
To get started with React Native:
267269
1. Follow the [Getting Started guide for React Native](/docs/connections/sources/catalog/libraries/mobile/react-native/).
@@ -273,7 +275,7 @@ To get started with React Native:
273275
274276
3. Run `npx typewriter init` to use the Typewriter quickstart wizard that generates a `typewriter.yml` configuration along with your first Typewriter client.
275277
276-
This command 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 / Segment client in `./analytics` (or whichever path you configured). You can use this interchangeably as a normal React Native Segment client. It contains additional methods for your tracking plan:
278+
This command creates a `typewriter.yml` file in your repository. For more information on the format of this file, see the [Typewriter Configuration Reference](#configuration-reference). The command also adds a new Typewriter/Segment client in `./analytics` (or whichever path you configured). You can use this interchangeably as a normal React Native Segment client. It contains additional methods for your tracking plan:
277279
278280
```ts
279281
import {
@@ -318,10 +320,11 @@ To get started with React Native:
318320
// Remember this is just an extended client with the typewriter methods so all the normal segment methods still work!
319321
segmentClient.track('Untyped event');
320322
```
323+
321324
> info ""
322325
> Run `npx typewriter` to regenerate your Typewriter client. You need to do this each time you update your Tracking Plan.
323326
324-
## Adding Events
327+
## Adding events
325328
326329
To update or add a new event to a Typewriter client, first apply your changes to your Tracking Plan. Then run the following:
327330
@@ -330,20 +333,20 @@ To update or add a new event to a Typewriter client, first apply your changes to
330333
$ npx typewriter
331334
```
332335
333-
## API Token Configuration
336+
## API token configuration
334337
335338
Typewriter requires a Segment API token to fetch Tracking Plans from the [Segment Public API](https://docs.segmentapis.com/){:target="_blank”}.
336339
337340
338-
You must be a workspace owner to create Segment API tokens.
341+
You must have the [Workspace Owner role](/docs/segment-app/iam/roles/) to create Segment API tokens.
339342
340343
To create an API token:
341344
1. Click on the **Tokens** tab on the [Access Management](https://app.segment.com/goto-my-workspace/settings/access-management){:target="_blank”} page and click **Create Token**.
342345
2. Choose Segment's Public API.
343346
3. Add a description for the token and assign access. If you choose *Workspace Member*, you only need to select **Tracking Plan Read-Only** for the Resource Role, as Typewriter only needs the *Tracking Plan Read-Only* role.
344347
4. Click **Create**.
345348
346-
Typewriter looks for an API token in three ways, in the following order:
349+
Typewriter looks for an API token in the following order:
347350
1. If a token is piped through, it will use that token. For example, `echo $TW_TOKEN | typewriter build`.
348351
2. Typewriter executes a token script from the `typewriter.yml`. See [Token Script](#token-script) for more information.
349352
3. Typewriter reads the contents of the `~/.typewriter` file.
@@ -401,7 +404,7 @@ If this isn't possible you can also check in the full generated client. Segment,
401404
402405
## Configuration Reference
403406
404-
Typewriter stores its configuration in a `typewriter.yml` file in the root of your repo. A sample configuration might look like this:
407+
Typewriter stores its configuration in a `typewriter.yml` file in the root of your repository. A sample configuration might look like this:
405408
406409
```yml
407410
# Segment Typewriter Configuration Reference (https://github.com/segmentio/typewriter)
@@ -566,7 +569,7 @@ typewriter.setTypewriterOptions({
566569
567570
## Known Limitations
568571
569-
Typewriter only supports `track` calls. However, you can continue to use the underlying (untyped) analytics instance to perform `identify`, `group`, `page`, `screen`, and `alias` calls.
572+
Typewriter only supports Track calls. However, you can continue to use the underlying (untyped) analytics instance to perform Identify, Group, Page, Screen, and Alias calls.
570573
571574
Not all languages support run-time validation. Currently, `analytics.js` and `analytics-node` support it using [AJV](https://github.com/epoberezkin/ajv){:target="_blank"} (both for JavaScript and TypeScript projects) while `analytics-swift` and `analytics-kotlin` don't support run-time validation. Typewriter also does not support event validation using the Common JSON Schema.
572575
@@ -576,4 +579,4 @@ If you're interested in contributing, [open an issue on GitHub](https://github.c
576579
577580
## Feedback
578581
579-
Segment welcomes feedback you may have on your experience with Typewriter. To contact Segment, [open an issue on GitHub](https://github.com/segmentio/typewriter/issues/new){:target="_blank”}.
582+
Segment welcomes feedback you may have on your experience with Typewriter. To contact us, [open an issue on GitHub](https://github.com/segmentio/typewriter/issues/new){:target="_blank”}.

0 commit comments

Comments
 (0)