-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(session replay): update targeting logic to be compatible with ne…
…w idb store format
- Loading branch information
Kelly Wallach
committed
Jun 12, 2024
1 parent
049d56c
commit 8a38a38
Showing
4 changed files
with
139 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/session-replay-browser/src/targeting-idb-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Logger as ILogger } from '@amplitude/analytics-types'; | ||
import { DBSchema, IDBPDatabase, openDB } from 'idb'; | ||
import { STORAGE_FAILURE } from './messages'; | ||
|
||
export interface SessionReplayTargetingDB extends DBSchema { | ||
sessionTargetingMatch: { | ||
key: number; | ||
value: { | ||
sessionId: number; | ||
targetingMatch: boolean; | ||
}; | ||
}; | ||
} | ||
|
||
export const createStore = async (dbName: string) => { | ||
return await openDB<SessionReplayTargetingDB>(dbName, 1, { | ||
upgrade: (db: IDBPDatabase<SessionReplayTargetingDB>) => { | ||
if (!db.objectStoreNames.contains('sessionTargetingMatch')) { | ||
db.createObjectStore('sessionTargetingMatch', { | ||
keyPath: 'sessionId', | ||
}); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
const openOrCreateDB = async (apiKey: string) => { | ||
const dbName = `${apiKey.substring(0, 10)}_amp_session_replay_targeting`; | ||
return await createStore(dbName); | ||
}; | ||
|
||
export const getTargetingMatchForSession = async ({ | ||
loggerProvider, | ||
apiKey, | ||
sessionId, | ||
}: { | ||
loggerProvider: ILogger; | ||
apiKey: string; | ||
sessionId: number; | ||
}) => { | ||
const db = await openOrCreateDB(apiKey); | ||
try { | ||
const targetingMatchForSession = await db?.get<'sessionTargetingMatch'>('sessionTargetingMatch', sessionId); | ||
|
||
return targetingMatchForSession?.targetingMatch; | ||
} catch (e) { | ||
loggerProvider.warn(`${STORAGE_FAILURE}: ${e as string}`); | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const storeTargetingMatchForSession = async ({ | ||
loggerProvider, | ||
apiKey, | ||
sessionId, | ||
targetingMatch, | ||
}: { | ||
loggerProvider: ILogger; | ||
apiKey: string; | ||
sessionId: number; | ||
targetingMatch: boolean; | ||
}) => { | ||
const db = await openOrCreateDB(apiKey); | ||
try { | ||
const targetingMatchForSession = await db?.put<'sessionTargetingMatch'>('sessionTargetingMatch', { | ||
targetingMatch, | ||
sessionId, | ||
}); | ||
|
||
return targetingMatchForSession; | ||
} catch (e) { | ||
loggerProvider.warn(`${STORAGE_FAILURE}: ${e as string}`); | ||
} | ||
return undefined; | ||
}; |