Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(session replay): add metadata to headers #598

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/plugin-session-replay-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"publish": "node ../../scripts/publish/upload-to-s3.js",
"test": "jest",
"typecheck": "tsc -p ./tsconfig.json",
"version": "yarn add @amplitude/analytics-types@\">=1 <3\" @amplitude/analytics-client-common@\">=1 <3\" @amplitude/analytics-core@\">=1 <3\""
"version": "yarn add @amplitude/analytics-types@\">=1 <3\" @amplitude/analytics-client-common@\">=1 <3\" @amplitude/analytics-core@\">=1 <3\"",
"version-file": "node -p \"'export const VERSION = \\'' + require('./package.json').version + '\\';'\" > src/version.ts"
},
"bugs": {
"url": "https://github.com/amplitude/Amplitude-TypeScript/issues"
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-session-replay-browser/src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERSION = '0.6.14';
3 changes: 2 additions & 1 deletion packages/session-replay-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"publish": "node ../../scripts/publish/upload-to-s3.js",
"test": "jest",
"typecheck": "tsc -p ./tsconfig.json",
"version": "yarn add @amplitude/analytics-types@\">=1 <3\" @amplitude/analytics-client-common@\">=1 <3\" @amplitude/analytics-core@\">=1 <3\""
"version": "yarn add @amplitude/analytics-types@\">=1 <3\" @amplitude/analytics-client-common@\">=1 <3\" @amplitude/analytics-core@\">=1 <3\"",
"version-file": "node -p \"'export const VERSION = \\'' + require('./package.json').version + '\\';'\" > src/version.ts"
},
"bugs": {
"url": "https://github.com/amplitude/Amplitude-TypeScript/issues"
Expand Down
1 change: 1 addition & 0 deletions packages/session-replay-browser/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const DEFAULT_EVENT_PROPERTY_PREFIX = '[Amplitude]';
export const DEFAULT_SESSION_REPLAY_PROPERTY = `${DEFAULT_EVENT_PROPERTY_PREFIX} Session Recorded`;
export const DEFAULT_SESSION_START_EVENT = 'session_start';
export const DEFAULT_SESSION_END_EVENT = 'session_end';
export const DEFAULT_SAMPLE_RATE = 1;
jxiwang marked this conversation as resolved.
Show resolved Hide resolved

export const BLOCK_CLASS = 'amp-block';
export const MASK_TEXT_CLASS = 'amp-mask';
Expand Down
5 changes: 5 additions & 0 deletions packages/session-replay-browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ export const isSessionInSample = function (sessionId: number, sampleRate: number
const mod = absHashMultiply % 100;
return mod / 100 < sampleRate;
};

export const getCurrentUrl = () => {
// eslint-disable-next-line no-restricted-globals
return typeof window !== 'undefined' ? window.location.href : '';
jxiwang marked this conversation as resolved.
Show resolved Hide resolved
};
11 changes: 10 additions & 1 deletion packages/session-replay-browser/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pack, record } from 'rrweb';
import { SessionReplayConfig } from './config';
import {
BLOCK_CLASS,
DEFAULT_SAMPLE_RATE,
DEFAULT_SESSION_REPLAY_PROPERTY,
MASK_TEXT_CLASS,
MAX_EVENT_LIST_SIZE_IN_BYTES,
Expand All @@ -17,7 +18,7 @@ import {
STORAGE_PREFIX,
defaultSessionStore,
} from './constants';
import { isSessionInSample, maskInputFn } from './helpers';
import { isSessionInSample, maskInputFn, getCurrentUrl } from './helpers';
import {
MAX_RETRIES_EXCEEDED_MESSAGE,
MISSING_API_KEY_MESSAGE,
Expand All @@ -37,6 +38,7 @@ import {
SessionReplayContext,
SessionReplayOptions,
} from './typings/session-replay';
import { VERSION } from './verstion';

export class SessionReplay implements AmplitudeSessionReplay {
name = '@amplitude/session-replay-browser';
Expand Down Expand Up @@ -384,6 +386,10 @@ export class SessionReplay implements AmplitudeSessionReplay {
return this.completeRequest({ context, err: MISSING_DEVICE_ID_MESSAGE });
}

const url = getCurrentUrl();
const version = VERSION;
const sampleRate = this.config?.sampleRate || DEFAULT_SAMPLE_RATE;

const urlParams = new URLSearchParams({
device_id: deviceId,
session_id: `${context.sessionId}`,
Expand All @@ -400,6 +406,9 @@ export class SessionReplay implements AmplitudeSessionReplay {
'Content-Type': 'application/json',
Accept: '*/*',
Authorization: `Bearer ${apiKey}`,
'X-Client-Version': version,
jxiwang marked this conversation as resolved.
Show resolved Hide resolved
'X-Client-Url': url,
'X-Client-Sample-Rate': `${sampleRate}`,
},
body: JSON.stringify(payload),
method: 'POST',
Expand Down
1 change: 1 addition & 0 deletions packages/session-replay-browser/src/verstion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERSION = '0.2.5';
jxiwang marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 17 additions & 2 deletions packages/session-replay-browser/test/session-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as Helpers from '../src/helpers';
import { UNEXPECTED_ERROR_MESSAGE, getSuccessMessage } from '../src/messages';
import { SessionReplay } from '../src/session-replay';
import { IDBStore, RecordingStatus, SessionReplayConfig, SessionReplayOptions } from '../src/typings/session-replay';
import { VERSION } from '../src/verstion';

jest.mock('idb-keyval');
type MockedIDBKeyVal = jest.Mocked<typeof import('idb-keyval')>;
Expand Down Expand Up @@ -1054,7 +1055,14 @@ describe('SessionReplayPlugin', () => {
'https://api-sr.amplitude.com/sessions/v2/track?device_id=1a2b3c&session_id=123&seq_number=1',
{
body: JSON.stringify({ version: 1, events: [mockEventString] }),
headers: { Accept: '*/*', 'Content-Type': 'application/json', Authorization: 'Bearer static_key' },
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
Authorization: 'Bearer static_key',
'X-Client-Sample-Rate': '1',
'X-Client-Url': window.location.href,
'X-Client-Version': VERSION,
},
method: 'POST',
},
);
Expand All @@ -1077,7 +1085,14 @@ describe('SessionReplayPlugin', () => {
'https://api-sr.eu.amplitude.com/sessions/v2/track?device_id=1a2b3c&session_id=123&seq_number=1',
{
body: JSON.stringify({ version: 1, events: [mockEventString] }),
headers: { Accept: '*/*', 'Content-Type': 'application/json', Authorization: 'Bearer static_key' },
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
Authorization: 'Bearer static_key',
'X-Client-Sample-Rate': '1',
'X-Client-Url': window.location.href,
'X-Client-Version': VERSION,
},
method: 'POST',
},
);
Expand Down
Loading