diff --git a/package.json b/package.json index 0dc2a79..0e867a3 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/actions.ts b/src/actions.ts new file mode 100644 index 0000000..dbe8c9e --- /dev/null +++ b/src/actions.ts @@ -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) +} + \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..7f5f6e9 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,3 @@ +import { setupSegmentActions } from "./actions"; + +export { setupSegmentActions };