|
1 | | -# Segment LDK |
| 1 | +# Loop Analytics Library |
2 | 2 |
|
3 | | -This wrapper library makes use of [Segment](https://segment.com/docs/) to send loop data to analytics services like GA and Mixpanel. Its intent is to make it easier for developers to add analytics to loops, reduce code bloat and most importantly to be consistent with event trigger names/labels. Feel free to change the types to match event trigger names specific for your use case. |
| 3 | +This library makes use of the LDK's Network aptitude to support analytics within loops through builtin analytics services or can be extended to support custom services |
4 | 4 |
|
5 | | -## How to use |
| 5 | +Currently supported out of the box: |
6 | 6 |
|
7 | | -In the root file that starts the loop, add the following ahead of prior code. |
| 7 | +- [Google Analytics (Universal)](https://developers.google.com/analytics/devguides/collection/protocol/v1) |
| 8 | +- [Segment](https://segment.com/docs/connections/sources/catalog/libraries/server/http-api/) |
8 | 9 |
|
9 | | -```js |
10 | | -import User from '../user'; |
11 | | -import Segment from 'loop-segment'; |
| 10 | +## How to Use |
12 | 11 |
|
13 | | - /* |
14 | | - * We are using userId from user.jwt() aptitude (part of @oliveai/ldk) which has been |
15 | | - * abstracted to a separate class User. But the Segment userId can be populated from other |
16 | | - * id sources to fit your use case. |
17 | | - */ |
18 | | - const { userId } = await User.getUser(); |
19 | | - Segment.init({ |
20 | | - loopName: 'KnowledgeReference Loop', |
21 | | - userId, |
22 | | - writeKey: 'this is found in segment sources settings', |
23 | | - }); |
24 | | -``` |
| 12 | +### 1. Add the necessary aptitude permissions to your loop's `package.json` |
25 | 13 |
|
26 | | -You should receive a Segment tracking event in your account, that says "Loop Started" |
| 14 | +The only required aptitude is `network` to make requests to the analytics service you choose. Some other aptitudes that can be helpful are `user` for getting the user's information and `system` for getting the user's operating system. |
27 | 15 |
|
28 | | -To use the rest of the methods, add them in where necessary in the code (e.g. `Segment.whisperDisplayed(Markdown, true)`). Be sure to update the `types` when creating new event names to be triggered in the loops. |
| 16 | +> Note: Use the information from `user` and `system` aptitudes at your own risk. PII and PHI should **NEVER** be sent to an analytics service, only the minimum required info that is helpful for metrics should be used. If you are unsure whether your information would be considered PII or PHI or you don't need this info for your metrics, err on the side of caution and omit these aptitudes altogether. |
29 | 17 |
|
30 | | -## Important Notes |
| 18 | +#### Network Aptitude URLs for the provided Transports |
31 | 19 |
|
32 | | -- Be sure to add the network and user permissions in your loop like so, |
| 20 | +| Transport | URL | |
| 21 | +| :-------: | :------------------: | |
| 22 | +| Google | google-analytics.com | |
| 23 | +| Segment | api.segment.io | |
33 | 24 |
|
34 | 25 | ```json |
35 | | - "network": { |
36 | | - "urlDomains": [ |
37 | | - { |
38 | | - "value": "api.segment.io" |
39 | | - } |
40 | | - ] |
41 | | - }, |
| 26 | +{ |
| 27 | + ... |
| 28 | + "ldk": { |
| 29 | + "permissions": { |
| 30 | + "whisper": {}, |
| 31 | + "system": {}, |
42 | 32 | "user": { |
43 | | - "optionalClaims": [ |
44 | | - { |
45 | | - "value": "email" |
46 | | - } |
47 | | - ] |
| 33 | + "optionalClaims": [{ "value": "email" }] |
| 34 | + }, |
| 35 | + "network": { |
| 36 | + "urlDomains": [{ "value": "google-analytics.com" }] |
48 | 37 | } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +### 2. Set up the Analytics Client with your desired Transport |
| 44 | + |
| 45 | +In this example we setup and export the Google Analytics client in a separate file to make it accessible across the loop |
| 46 | + |
| 47 | +```ts |
| 48 | +// analytics.ts |
| 49 | +import { user, system } from '@oliveai/ldk'; |
| 50 | +import { AnalyticsClient, Transports } from '@oliveai/loop-analytics'; |
| 51 | +import jwtDecode from 'jwt-decode'; |
| 52 | + |
| 53 | +import { LOOP_NAME } from './index'; |
| 54 | + |
| 55 | +const createAnalyticsClient = async () => { |
| 56 | + // Helper function to fetch the user's JWT and decode the payload |
| 57 | + const getUserInfo = async () => |
| 58 | + jwtDecode<{ email: string; org: string; sub: string }>( |
| 59 | + await user.jwt({ includeEmail: true }) |
| 60 | + ); |
| 61 | + |
| 62 | + // Get the info we need from LDK |
| 63 | + const { sub: userId, email, org } = await getUserInfo(); |
| 64 | + const os = await system.operatingSystem(); |
| 65 | + |
| 66 | + // Define required and custom category/action pairings |
| 67 | + // Export outside of this function to use across the loop for custom events if you'd like |
| 68 | + const categoryActionMap: Transports.GoogleTransportTypes.CategoryActionMap = { |
| 69 | + // === Required === |
| 70 | + whisperDisplayed: { |
| 71 | + category: 'Whispers', |
| 72 | + action: 'Whisper Displayed', |
| 73 | + }, |
| 74 | + whisperClosed: { |
| 75 | + category: 'Whispers', |
| 76 | + action: 'Whisper Closed', |
| 77 | + }, |
| 78 | + componentClicked: { |
| 79 | + category: 'Click Events', |
| 80 | + action: 'Component Clicked', |
| 81 | + }, |
| 82 | + componentCopied: { |
| 83 | + category: 'Click Events', |
| 84 | + action: 'Component Copied', |
| 85 | + }, |
| 86 | + // === Custom (Optional) === |
| 87 | + customEvent: { |
| 88 | + category: 'Custom', |
| 89 | + action: 'Custom Event', |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + // An example of how to set custom dimensions, use whatever is appropriate |
| 94 | + const customDimensions: Transports.GoogleTransportTypes.CustomDimensionOrMetric[] = |
| 95 | + [ |
| 96 | + { |
| 97 | + index: 1, |
| 98 | + name: 'Loop Name', |
| 99 | + value: LOOP_NAME, |
| 100 | + }, |
| 101 | + { |
| 102 | + index: 2, |
| 103 | + name: 'User ID', |
| 104 | + value: userId, // The user's generated ID in Loop Library, not considered PII |
| 105 | + }, |
| 106 | + { |
| 107 | + index: 3, |
| 108 | + name: 'Email Domain', |
| 109 | + value: email.split('@')[1], // ONLY send the email domain, full email would be PII |
| 110 | + }, |
| 111 | + { |
| 112 | + index: 4, |
| 113 | + name: 'Organization', |
| 114 | + value: org, |
| 115 | + }, |
| 116 | + { |
| 117 | + index: 5, |
| 118 | + name: 'Operating System', |
| 119 | + value: os, |
| 120 | + }, |
| 121 | + ]; |
| 122 | + |
| 123 | + const transportConfig: Transports.GoogleTransportTypes.GoogleTransportConfig = |
| 124 | + { |
| 125 | + trackingId: 'UA-12345678-90', |
| 126 | + // Can be whatever you want but is required for Google's request headers |
| 127 | + userAgent: LOOP_NAME, |
| 128 | + categoryActionMap, |
| 129 | + customDimensions, |
| 130 | + }; |
| 131 | + |
| 132 | + const userConfig: Transports.UserConfig = { |
| 133 | + id: userId, |
| 134 | + }; |
| 135 | + const loopConfig: Transports.LoopConfig = { |
| 136 | + name: LOOP_NAME, |
| 137 | + }; |
| 138 | + |
| 139 | + return new AnalyticsClient( |
| 140 | + new Transports.GoogleTransport(transportConfig, userConfig, loopConfig) |
| 141 | + ); |
| 142 | +}; |
| 143 | + |
| 144 | +// One example of how to export the client in a way that is accessible across the loop |
| 145 | +export default await createAnalyticsClient(); |
| 146 | +``` |
| 147 | + |
| 148 | +### 3. Use the new Analytics Client wherever it's needed |
| 149 | + |
| 150 | +```ts |
| 151 | +// index.ts |
| 152 | +import { whisper } from '@oliveai/ldk'; |
| 153 | +import GA from './analytics'; |
| 154 | + |
| 155 | +export const LOOP_NAME = 'My Awesome Loop'; |
| 156 | + |
| 157 | +(async () => { |
| 158 | + const label = 'Hello World!'; |
| 159 | + |
| 160 | + whisper.create({ |
| 161 | + label, |
| 162 | + components: [], |
| 163 | + onClose: () => GA.trackWhisperClosed(label), |
| 164 | + }); |
| 165 | + |
| 166 | + GA.trackWhisperDisplayed(label, false); |
| 167 | +})(); |
49 | 168 | ``` |
0 commit comments