|
| 1 | +# `@scaleway/use-segment` |
| 2 | + |
| 3 | +## A tiny hooks to handle segment events |
| 4 | + |
| 5 | +use [@segment/analytics-next](https://github.com/segmentio/analytics-next) |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +$ yarn add @scaleway/use-segment |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Event directory |
| 16 | + |
| 17 | +Create an events directory with all you specific events. |
| 18 | + |
| 19 | +``` |
| 20 | +events |
| 21 | + ┣ pageTypes |
| 22 | + ┃ ┗ index.ts |
| 23 | + ┣ 📂loginEvent |
| 24 | + ┃ ┗ index.ts |
| 25 | + ┃ index.ts ( export all you functions ) |
| 26 | +
|
| 27 | +``` |
| 28 | + |
| 29 | +Each event will have a format like this: |
| 30 | + |
| 31 | +```typescript |
| 32 | +const pageVisited = |
| 33 | + (analytics?: Analytics) => |
| 34 | + async (args: Args): Promise<void> => { |
| 35 | + // here do what you have to do with analytics |
| 36 | + await analytics?.page(args) |
| 37 | + } |
| 38 | + |
| 39 | +export default pageVisited |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +```typescript |
| 45 | + |
| 46 | +import pageTypes from './pageTypes' |
| 47 | +import testEvents from './testEvents' |
| 48 | + |
| 49 | +export default { |
| 50 | + pageTypes, |
| 51 | + testEvents, |
| 52 | +} |
| 53 | + |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +### Context Load |
| 58 | + |
| 59 | +Inside you global app you have to use our Segment Provider to allow loading of segment from your settting app. |
| 60 | +This will trigger a load and return analitycs function inside you provider. |
| 61 | + |
| 62 | +```javascript |
| 63 | +import SegmentProvider from '@scaleway/use-segment' |
| 64 | +import { captureMessage } from '@sentry/browser' |
| 65 | +import events from './events' |
| 66 | + |
| 67 | +const App = () => ( |
| 68 | + <SegmentProvider |
| 69 | + settings={{ writeKey: 'WRITE_KEY' }} // check your segment writeKey |
| 70 | + cdn="https://cdn.url" // In case you are using a proxy cdn |
| 71 | + events={events} |
| 72 | + onError={e => captureMessage(`Error on Segment: ${e.message}`)} |
| 73 | + > |
| 74 | + <App /> |
| 75 | + </SegmentProvider> |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +### Hook utility |
| 80 | + |
| 81 | +Now you maybe want to use your events inside your app . |
| 82 | +If you are using typescript, you may |
| 83 | + |
| 84 | +```typescript |
| 85 | +import { useSegment } from '@scaleway/use-segment' |
| 86 | +import type { Events } from 'types/events' |
| 87 | +import { Form, Submit } from '@scaleway/form' |
| 88 | + |
| 89 | +const Login = () => { |
| 90 | + const { events } = useSegment<Events>() |
| 91 | + |
| 92 | + const onSubmit = async args => { |
| 93 | + // make you api calls |
| 94 | + await events.login() |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <Form onSubmit={onSubmit}> |
| 99 | + // others fields |
| 100 | + <Submit /> |
| 101 | + </Form> |
| 102 | + ) |
| 103 | +} |
| 104 | +``` |
0 commit comments