Skip to content

Commit

Permalink
feat: Create base segment actions plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Wallach committed Jun 27, 2024
1 parent 6853996 commit c99304f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
"author": "Amplitude Inc",
"license": "MIT",
"dependencies": {
"@amplitude/session-replay-browser": "^1.9.1",
"@amplitude/session-replay-browser": "1.9.4",
"@segment/analytics-next": "^1.70.0",
"cookiejs": "^2.1.3",
"dev": "^0.1.3",
"tslib": "^2.4.1"
},
"scripts": {
"build": "yarn build:fix-finder && yarn bundle && yarn build:es5 && yarn build:esm",
"build": "yarn bundle && yarn build:es5 && yarn build:esm",
"bundle": "rollup --config scripts/build/rollup.config.js",
"bundle:watch": "yarn build:fix-finder && yarn bundle --watch && yarn build:esm --watch",
"bundle:watch": "yarn bundle --watch && yarn build:esm --watch",
"build:es5": "tsc -p ./tsconfig.es5.json",
"build:esm": "tsc -p ./tsconfig.esm.json",
"build:fix-finder": "rimraf node_modules/@medv/finder/finder.ts # This is required until the package is fixed upstream",
"dev": "nodemon --watch src --watch test --ext ts,json --exec 'yarn bundle && yarn start'",
"debug": "yarn bundle && node --enable-source-maps --inspect-brk ./dist/index.js",
"debug:test": "node --inspect-brk ./node_modules/.bin/jest --runInBand .",
"format": "prettier . --write",
"format:check": "prettier . --check",
"lint": "eslint . --fix",
"lint:check": "eslint .",
"start": "node --enable-source-maps ./dist/index.js",
"test": "jest --runInBand ."
},
"devDependencies": {
Expand Down
72 changes: 72 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { LogLevel } from "@amplitude/analytics-types";
// @ts-ignore
import * as sessionReplay from "@amplitude/session-replay-browser";
// @ts-ignore
import { SessionReplayOptions } from "@amplitude/session-replay-browser";
import { AnalyticsBrowser, Plugin } from "@segment/analytics-next";
import cookie from 'cookiejs';

const getStoredSessionId = () => {
const storedId = cookie.get("amp_session_id") as string;
console.log('storedId', storedId)
if (storedId) {
return parseInt(storedId, 10);
}
return undefined
}

type AmplitudeIntegrationData = {
session_id: number;
}

export const setupSegmentActions = async ({
amplitudeApiKey,
segmentInstance,
sessionReplayOptions,
}: {
amplitudeApiKey: string,
segmentInstance: AnalyticsBrowser,
sessionReplayOptions: SessionReplayOptions,
}) => {
const sessionReplayPlugin: Plugin = {
name: 'Session Replay Events',
type: 'enrichment',
version: '1.0.0',

isLoaded: () => true,
load:async (_ctx, ajs) => {
const user = ajs.user()
const storedSessionId = getStoredSessionId();

await sessionReplay.init(amplitudeApiKey, {
sessionId: storedSessionId,
deviceId: user.anonymousId() as string,
logLevel: LogLevel.Debug,
...sessionReplayOptions
}).promise;
},

track: async (ctx) => {
const storedSessionId = getStoredSessionId() || 0;
const amplitudeIntegrationData = ctx.event.integrations && ctx.event.integrations["Actions Amplitude"] as AmplitudeIntegrationData;
const nextSessionId = amplitudeIntegrationData?.session_id;
console.log('nextSessionId', nextSessionId, 'storedSessionId', storedSessionId)
if (nextSessionId && storedSessionId < nextSessionId) {
cookie.set('amp_session_id', nextSessionId.toString());
await sessionReplay.setSessionId(nextSessionId).promise;
}

// await sessionReplay.evaluateTargetingAndRecord({ event: ctx.event });
const sessionReplayProperties = sessionReplay.getSessionReplayProperties();
const properties = {
...ctx.event.properties,
...sessionReplayProperties
}
ctx.updateEvent('properties', properties)
return ctx
}
}

await segmentInstance.register(sessionReplayPlugin)
}

3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { setupSegmentActions } from "./actions";

export { setupSegmentActions };

0 comments on commit c99304f

Please sign in to comment.