@amplitude/segment-session-replay-plugin-react-native 0.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/package.json +78 -0
- package/src/index.ts +1 -0
- package/src/segment-session-replay-plugin.ts +93 -0
- package/src/version.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Amplitude Analytics
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @amplitude/segment-session-replay-plugin-react-native
|
|
2
|
+
|
|
3
|
+
Amplitude Segment Session Replay Plugin for React Native
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @amplitude/segment-session-replay-plugin-react-native
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
This plugin requires the following dependencies to be installed:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @amplitude/session-replay-react-native @segment/analytics-react-native
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Important**: This plugin requires the `@segment/analytics-react-native-plugin-amplitude-session` plugin to extract session IDs from Amplitude integration data. Make sure to add this plugin to your Segment client **before** adding the session replay plugin.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Segment Session Replay Plugin
|
|
24
|
+
|
|
25
|
+
Add the segment session replay plugin to your Segment Analytics instance to automatically integrate Amplitude Session Replay with your Segment events.
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { createSegmentSessionReplayPlugin } from '@amplitude/segment-session-replay-plugin-react-native';
|
|
29
|
+
import { createClient } from '@segment/analytics-react-native';
|
|
30
|
+
import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude-session';
|
|
31
|
+
|
|
32
|
+
// Initialize Segment client
|
|
33
|
+
const segmentClient = createClient({
|
|
34
|
+
writeKey: 'YOUR_SEGMENT_WRITE_KEY',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Configure session replay plugin
|
|
38
|
+
const sessionReplayConfig = {
|
|
39
|
+
apiKey: 'YOUR_AMPLITUDE_API_KEY',
|
|
40
|
+
deviceId: 'YOUR_DEVICE_ID'
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Add the Amplitude session plugin first (required for session ID extraction)
|
|
44
|
+
await segmentClient.add({ plugin: new AmplitudeSessionPlugin() });
|
|
45
|
+
|
|
46
|
+
// Add the session replay plugin to Segment
|
|
47
|
+
await segmentClient.add(createSegmentSessionReplayPlugin(sessionReplayConfig));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Plugin Configuration
|
|
51
|
+
|
|
52
|
+
The plugin accepts a `SessionReplayConfig` object
|
|
53
|
+
|
|
54
|
+
### Automatic Integration
|
|
55
|
+
|
|
56
|
+
The plugin automatically:
|
|
57
|
+
|
|
58
|
+
1. **Initializes Session Replay**: Sets up Amplitude Session Replay with your configuration
|
|
59
|
+
2. **Syncs Session Data**: Updates session ID and device ID for each Segment event
|
|
60
|
+
3. **Enriches Events**: Adds session replay properties to track and screen events
|
|
61
|
+
4. **Manages Lifecycle**: Handles start/stop operations for session replay
|
|
62
|
+
|
|
63
|
+
### Event Processing
|
|
64
|
+
|
|
65
|
+
The plugin processes the following Segment event types:
|
|
66
|
+
- `TrackEvent`: Adds session replay properties to track events
|
|
67
|
+
- `ScreenEvent`: Adds session replay properties to screen events
|
|
68
|
+
|
|
69
|
+
For these events, the plugin:
|
|
70
|
+
- Extracts session ID from event properties or Amplitude integration data
|
|
71
|
+
- Extracts device ID from event context or anonymous ID
|
|
72
|
+
- Adds session replay properties to the event before sending to Segment
|
|
73
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amplitude/segment-session-replay-plugin-react-native",
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
|
+
"description": "Amplitude Segment Session Replay Plugin for React Native",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"analytics",
|
|
7
|
+
"amplitude",
|
|
8
|
+
"react-native",
|
|
9
|
+
"ios",
|
|
10
|
+
"android",
|
|
11
|
+
"session-replay"
|
|
12
|
+
],
|
|
13
|
+
"author": "Amplitude Inc",
|
|
14
|
+
"homepage": "https://github.com/amplitude/Amplitude-TypeScript",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"main": "lib/commonjs/index",
|
|
17
|
+
"module": "lib/module/index",
|
|
18
|
+
"types": "lib/typescript/index.d.ts",
|
|
19
|
+
"react-native": "src/index.ts",
|
|
20
|
+
"source": "src/index.ts",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"tag": "latest"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/amplitude/Amplitude-TypeScript.git"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "bob build",
|
|
31
|
+
"fix": "yarn fix:eslint & yarn fix:prettier",
|
|
32
|
+
"fix:eslint": "eslint '{src,test}/**/*.ts' --fix",
|
|
33
|
+
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
|
|
34
|
+
"lint": "yarn lint:prettier && yarn lint:eslint",
|
|
35
|
+
"lint:eslint": "eslint '{src,test}/**/*.ts'",
|
|
36
|
+
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
|
|
37
|
+
"test": "jest",
|
|
38
|
+
"typecheck": "tsc -p ./tsconfig.json",
|
|
39
|
+
"version": "yarn version-file && yarn build",
|
|
40
|
+
"version-file": "node -p \"'export const VERSION = \\'' + require('./package.json').version + '\\';'\" > src/version.ts",
|
|
41
|
+
"typescript": "tsc --noEmit"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"src",
|
|
45
|
+
"!test",
|
|
46
|
+
"!**/__tests__",
|
|
47
|
+
"!**/__fixtures__",
|
|
48
|
+
"!**/__mocks__",
|
|
49
|
+
"!**/.*"
|
|
50
|
+
],
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/amplitude/Amplitude-TypeScript/issues"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@amplitude/session-replay-react-native": ">=0.0.1-beta.2 <1.0.0",
|
|
56
|
+
"@segment/analytics-react-native": "2.15.0",
|
|
57
|
+
"react-native-builder-bob": "^0.20.3"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"@amplitude/session-replay-react-native": "*",
|
|
61
|
+
"@segment/analytics-react-native": ">= 2.2.0 < 3"
|
|
62
|
+
},
|
|
63
|
+
"react-native-builder-bob": {
|
|
64
|
+
"source": "src",
|
|
65
|
+
"output": "lib",
|
|
66
|
+
"targets": [
|
|
67
|
+
"commonjs",
|
|
68
|
+
"module",
|
|
69
|
+
[
|
|
70
|
+
"typescript",
|
|
71
|
+
{
|
|
72
|
+
"project": "tsconfig.build.json"
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
]
|
|
76
|
+
},
|
|
77
|
+
"gitHead": "1ff69489af101b6a8780786b54a1caf403a4a425"
|
|
78
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createSegmentSessionReplayPlugin } from './segment-session-replay-plugin';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Plugin, PluginType, type SegmentEvent, EventType, SegmentClient } from '@segment/analytics-react-native';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type SessionReplayConfig,
|
|
5
|
+
getSessionReplayProperties,
|
|
6
|
+
init,
|
|
7
|
+
setDeviceId,
|
|
8
|
+
setSessionId,
|
|
9
|
+
start,
|
|
10
|
+
stop,
|
|
11
|
+
} from '@amplitude/session-replay-react-native';
|
|
12
|
+
import { VERSION } from './version';
|
|
13
|
+
|
|
14
|
+
function getSessionId(event: SegmentEvent): number {
|
|
15
|
+
const amplitudeSessionId =
|
|
16
|
+
(event.integrations?.['Actions Amplitude'] as { session_id: number })?.['session_id'] ?? null;
|
|
17
|
+
if (amplitudeSessionId !== null) {
|
|
18
|
+
return amplitudeSessionId;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (event.type === EventType.TrackEvent || event.type === EventType.ScreenEvent) {
|
|
22
|
+
const sessionIdRaw = event.properties?.['session_id'];
|
|
23
|
+
const sessionId = Number(sessionIdRaw);
|
|
24
|
+
return Number.isNaN(sessionId) ? -1 : sessionId;
|
|
25
|
+
}
|
|
26
|
+
return -1;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getDeviceId(event: SegmentEvent): string | null {
|
|
30
|
+
return event.context?.device?.id ?? event.anonymousId ?? null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class SegmentSessionReplayPlugin extends Plugin {
|
|
34
|
+
name = 'amplitude-segment-session-replay-plugin-react-native';
|
|
35
|
+
version: string = VERSION;
|
|
36
|
+
type: PluginType = PluginType.enrichment;
|
|
37
|
+
|
|
38
|
+
private sessionReplayConfig: SessionReplayConfig;
|
|
39
|
+
|
|
40
|
+
// @review: This is to ensure the plugin is initialized before the first event is processed.
|
|
41
|
+
// because `configure` is not asynchronous
|
|
42
|
+
private initPromise: Promise<void> | null = null;
|
|
43
|
+
|
|
44
|
+
constructor(config: SessionReplayConfig) {
|
|
45
|
+
super();
|
|
46
|
+
this.sessionReplayConfig = config;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async configure(analytics: SegmentClient): Promise<void> {
|
|
50
|
+
super.configure(analytics);
|
|
51
|
+
this.initPromise = init({
|
|
52
|
+
deviceId: analytics.userInfo.get().anonymousId,
|
|
53
|
+
...this.sessionReplayConfig,
|
|
54
|
+
});
|
|
55
|
+
await this.initPromise;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async execute(event: SegmentEvent): Promise<SegmentEvent> {
|
|
59
|
+
await this.initPromise;
|
|
60
|
+
|
|
61
|
+
const sessionId = getSessionId(event);
|
|
62
|
+
const deviceId = getDeviceId(event);
|
|
63
|
+
|
|
64
|
+
await setSessionId(sessionId);
|
|
65
|
+
await setDeviceId(deviceId);
|
|
66
|
+
|
|
67
|
+
if (event.type === EventType.TrackEvent || event.type === EventType.ScreenEvent) {
|
|
68
|
+
const properties = await getSessionReplayProperties();
|
|
69
|
+
event.properties = { ...event.properties, ...properties };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return event;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async shutdown(): Promise<void> {
|
|
76
|
+
await this.initPromise;
|
|
77
|
+
await stop();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async start(): Promise<void> {
|
|
81
|
+
await this.initPromise;
|
|
82
|
+
await start();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async stop(): Promise<void> {
|
|
86
|
+
await this.initPromise;
|
|
87
|
+
await stop();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function createSegmentSessionReplayPlugin(config: SessionReplayConfig): Plugin {
|
|
92
|
+
return new SegmentSessionReplayPlugin(config);
|
|
93
|
+
}
|
package/src/version.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = '0.0.1-beta.2';
|