Skip to content

Commit

Permalink
feat: Save the $screen_name on every event (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed May 25, 2023
1 parent 32d74d2 commit b70f194
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
22 changes: 18 additions & 4 deletions posthog-core/src/index.ts
Expand Up @@ -584,6 +584,7 @@ export abstract class PostHogCore extends PostHogCoreStateless {
// internal
protected _decideResponsePromise?: Promise<PostHogDecideResponse | undefined> // TODO: come back to this, fix typing
protected _sessionExpirationTimeSeconds: number
protected sessionProps: PostHogEventProperties = {}

constructor(apiKey: string, options?: PosthogCoreOptions) {
// Default for stateful mode is to not disable geoip. Only override if explicitly set
Expand Down Expand Up @@ -632,6 +633,7 @@ export abstract class PostHogCore extends PostHogCoreStateless {

private clearProps(): void {
this.props = undefined
this.sessionProps = {}
}

private _props: PostHogEventProperties | undefined
Expand Down Expand Up @@ -669,9 +671,10 @@ export abstract class PostHogCore extends PostHogCoreStateless {
}
}

private enrichProperties(properties?: PostHogEventProperties): any {
public enrichProperties(properties?: PostHogEventProperties): any {
return {
...this.props, // Persisted properties first
...this.sessionProps, // Followed by session properties
...(properties || {}), // Followed by user specified properties
...this.getCommonEventProperties(), // Followed by FF props
$session_id: this.getSessionId(),
Expand Down Expand Up @@ -707,6 +710,11 @@ export abstract class PostHogCore extends PostHogCoreStateless {
return this.getPersistedProperty<string>(PostHogPersistedProperty.DistinctId) || this.getAnonymousId()
}

unregister(property: string): void {
delete this.props[property]
this.setPersistedProperty<PostHogEventProperties>(PostHogPersistedProperty.Props, this.props)
}

register(properties: { [key: string]: any }): void {
this.props = {
...this.props,
Expand All @@ -715,9 +723,15 @@ export abstract class PostHogCore extends PostHogCoreStateless {
this.setPersistedProperty<PostHogEventProperties>(PostHogPersistedProperty.Props, this.props)
}

unregister(property: string): void {
delete this.props[property]
this.setPersistedProperty<PostHogEventProperties>(PostHogPersistedProperty.Props, this.props)
registerForSession(properties: { [key: string]: any }): void {
this.sessionProps = {
...this.sessionProps,
...properties,
}
}

unregisterForSession(property: string): void {
delete this.sessionProps[property]
}

/***
Expand Down
42 changes: 42 additions & 0 deletions posthog-core/test/posthog.register.spec.ts
@@ -0,0 +1,42 @@
import { PostHogPersistedProperty } from '../src'
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from './test-utils/PostHogCoreTestClient'

describe('PostHog Core', () => {
let posthog: PostHogCoreTestClient
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let mocks: PostHogCoreTestClientMocks

beforeEach(() => {
;[posthog, mocks] = createTestClient('TEST_API_KEY', {})
})

describe('register', () => {
it('should register properties to storage', () => {
posthog.register({ foo: 'bar' })
expect(posthog.enrichProperties()).toMatchObject({ foo: 'bar' })
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ foo: 'bar' })
posthog.register({ foo2: 'bar2' })
expect(posthog.enrichProperties()).toMatchObject({ foo: 'bar', foo2: 'bar2' })
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ foo: 'bar', foo2: 'bar2' })
})

it('should unregister properties from storage', () => {
posthog.register({ foo: 'bar', foo2: 'bar2' })
posthog.unregister('foo')
expect(posthog.enrichProperties().foo).toBeUndefined()
expect(posthog.enrichProperties().foo2).toEqual('bar2')
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ foo2: 'bar2' })
})

it('should register properties only for the session', () => {
posthog.registerForSession({ foo: 'bar' })
expect(posthog.enrichProperties()).toMatchObject({ foo: 'bar' })
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual(undefined)

posthog.register({ foo: 'bar2' })
expect(posthog.enrichProperties()).toMatchObject({ foo: 'bar' })
posthog.unregisterForSession('foo')
expect(posthog.enrichProperties()).toMatchObject({ foo: 'bar2' })
})
})
})
4 changes: 4 additions & 0 deletions posthog-react-native/CHANGELOG.md
@@ -1,3 +1,7 @@
# 2.7.0 - 2023-05-25

1. The `$screen_name` property will be registered for all events whenever `screen` is called

# 2.7.0 - 2023-04-20

1. Fixes a race condition that could occur when initialising PostHog
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/package.json
@@ -1,6 +1,6 @@
{
"name": "posthog-react-native",
"version": "2.7.0",
"version": "2.7.1",
"main": "lib/posthog-react-native/index.js",
"files": [
"lib/"
Expand Down
5 changes: 5 additions & 0 deletions posthog-react-native/src/posthog-rn.ts
Expand Up @@ -146,6 +146,11 @@ export class PostHog extends PostHogCore {

// Custom methods
screen(name: string, properties?: any): this {
// Screen name is good to know for all other subsequent events
this.registerForSession({
$screen_name: name,
})

return this.capture('$screen', {
...properties,
$screen_name: name,
Expand Down
@@ -1,3 +1,4 @@
import { PostHogPersistedProperty } from 'posthog-core'
import { PostHog, PostHogCustomAsyncStorage } from '../index'

describe('PostHog React Native', () => {
Expand Down Expand Up @@ -136,4 +137,21 @@ describe('PostHog React Native', () => {

await otherPostHog.shutdownAsync()
})

describe('screen', () => {
it('should set a $screen_name property on screen', async () => {
posthog = await PostHog.initAsync('test-token', {
customAsyncStorage: mockStorage,
flushInterval: 0,
})

posthog.screen('test-screen')

expect(posthog.enrichProperties()).toMatchObject({
$screen_name: 'test-screen',
})

expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual(undefined)
})
})
})

0 comments on commit b70f194

Please sign in to comment.