Skip to content

Commit

Permalink
Add client-side session params
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Oct 30, 2023
1 parent bf81856 commit fb3534d
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 57 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
"fflate": "^0.4.1"
},
"devDependencies": {
"eslint-config-posthog-js": "link:./eslint-rules",
"eslint-plugin-posthog-js": "link:./eslint-rules",
"@babel/core": "7.18.9",
"@babel/preset-env": "7.18.9",
"@babel/preset-typescript": "^7.18.6",
Expand All @@ -57,9 +55,11 @@
"babel-jest": "^26.6.3",
"cypress": "10.3.1",
"eslint": "8.20.0",
"eslint-config-posthog-js": "link:./eslint-rules",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-compat": "^4.1.4",
"eslint-plugin-jest": "^27.2.3",
"eslint-plugin-posthog-js": "link:./eslint-rules",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
Expand All @@ -74,7 +74,7 @@
"localStorage": "1.0.4",
"msw": "^1.2.1",
"node-fetch": "^2.6.1",
"posthog-js": "link:.",
"posthog-js": "file:.yalc/posthog-js",
"prettier": "^2.7.1",
"rollup": "^2.77.0",
"rollup-plugin-dts": "^4.2.2",
Expand Down
46 changes: 23 additions & 23 deletions playground/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "nextjs",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@lottiefiles/react-lottie-player": "^3.5.3",
"@next/font": "13.1.6",
"@types/node": "18.13.0",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.10",
"eslint": "8.34.0",
"eslint-config-next": "13.1.6",
"next": "13.1.6",
"posthog-js": "^1.45.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "4.9.5"
}
"name": "nextjs",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@lottiefiles/react-lottie-player": "^3.5.3",
"@next/font": "13.1.6",
"@types/node": "18.13.0",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.10",
"eslint": "8.34.0",
"eslint-config-next": "13.1.6",
"next": "13.1.6",
"posthog-js": "file:.yalc/posthog-js",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "4.9.5"
}
}
171 changes: 155 additions & 16 deletions src/__tests__/sessionid.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,21 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [
given.timestamp,
'newUUID',
given.timestamp,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -51,9 +63,21 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [
given.timestamp,
'newUUID',
given.timestamp,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -72,9 +96,21 @@ describe('Session ID manager', () => {
windowId: 'oldWindowID',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestampOfSessionStart,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestampOfSessionStart],
[SESSION_ID]: [
given.timestamp,
'oldSessionID',
given.timestampOfSessionStart,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
})

Expand All @@ -90,9 +126,21 @@ describe('Session ID manager', () => {
windowId: 'oldWindowID',
sessionId: 'oldSessionID',
sessionStartTimestamp: sessionStart,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [oldTimestamp, 'oldSessionID', sessionStart],
[SESSION_ID]: [
oldTimestamp,
'oldSessionID',
sessionStart,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
})

Expand All @@ -102,9 +150,21 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestampOfSessionStart,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestampOfSessionStart],
[SESSION_ID]: [
given.timestamp,
'oldSessionID',
given.timestampOfSessionStart,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -117,9 +177,21 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [
given.timestamp,
'newUUID',
given.timestamp,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -134,10 +206,22 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})

expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [
given.timestamp,
'newUUID',
given.timestamp,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -153,10 +237,22 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})

expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [
given.timestamp,
'newUUID',
given.timestamp,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -170,9 +266,21 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: now,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.now, 'newUUID', given.now],
[SESSION_ID]: [
given.now,
'newUUID',
given.now,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
})

Expand All @@ -182,9 +290,21 @@ describe('Session ID manager', () => {
windowId: 'oldWindowID',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestamp,
sessionSourceParams: {
initialPathName: '/',
referringDomain: '$direct',
},
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestamp],
[SESSION_ID]: [
given.timestamp,
'oldSessionID',
given.timestamp,
{
initialPathName: '/',
referringDomain: '$direct',
},
],
})
})
})
Expand All @@ -211,22 +331,41 @@ describe('Session ID manager', () => {

describe('session id storage', () => {
it('stores and retrieves a session id and timestamp', () => {
given.sessionIdManager._setSessionId('newSessionId', 1603107460000, 1603107460000)
given.sessionIdManager._setSessionId('newSessionId', 1603107460000, 1603107460000, {
initialPathName: '/some/path/name',
referringDomain: 'referring.example.com',
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [1603107460000, 'newSessionId', 1603107460000],
[SESSION_ID]: [
1603107460000,
'newSessionId',
1603107460000,
{
initialPathName: '/some/path/name',
referringDomain: 'referring.example.com',
},
],
})
expect(given.sessionIdManager._getSessionId()).toEqual([1603107460000, 'newSessionId', 1603107460000])
expect(given.sessionIdManager._getSessionId()).toEqual([
1603107460000,
'newSessionId',
1603107460000,
{
initialPathName: '/some/path/name',
referringDomain: 'referring.example.com',
},
])
})
})

describe('reset session id', () => {
it('clears the existing session id', () => {
given.sessionIdManager.resetSessionId()
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [null, null, null] })
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [null, null, null, null] })
})
it('a new session id is generated when called', () => {
given('storedSessionIdData', () => [null, null, null])
expect(given.sessionIdManager._getSessionId()).toEqual([null, null, null])
given('storedSessionIdData', () => [null, null, null, null])
expect(given.sessionIdManager._getSessionId()).toEqual([null, null, null, null])
expect(given.subject).toMatchObject({
windowId: 'newUUID',
sessionId: 'newUUID',
Expand Down
14 changes: 13 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,21 @@ export class PostHog {
const infoProperties = _info.properties()

if (this.sessionManager) {
const { sessionId, windowId } = this.sessionManager.checkAndGetSessionAndWindowId()
const { sessionId, windowId, sessionSourceParams } = this.sessionManager.checkAndGetSessionAndWindowId()
properties['$session_id'] = sessionId
properties['$window_id'] = windowId
if (
sessionSourceParams &&
(event_name === '$pageview' || event_name === '$pageleave' || event_name === '$autocapture')
) {
properties['$client_session_referring_host'] = sessionSourceParams.referringDomain
properties['$client_session_initial_pathname'] = sessionSourceParams.initialPathName
properties['$client_session_utm_source'] = sessionSourceParams.utmSource
properties['$client_session_utm_campaign'] = sessionSourceParams.utmCampaign
properties['$client_session_utm_medium'] = sessionSourceParams.utmMedium
properties['$client_session_utm_content'] = sessionSourceParams.utmContent
properties['$client_session_utm_term'] = sessionSourceParams.utmTerm
}
}

if (this.config.__preview_measure_pageview_stats) {
Expand Down
Loading

0 comments on commit fb3534d

Please sign in to comment.