Skip to content

Commit

Permalink
fix(decide): Make sure all stored properties are sent on first decide…
Browse files Browse the repository at this point in the history
… request (#633)
  • Loading branch information
neilkakkar committed May 17, 2023
1 parent 5e9439d commit bb057aa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cypress/e2e/capture.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ describe('Event capture', () => {
company: 'id:5',
playlist: 'id:77',
},
group_properties: {
company: { id: 5, company_name: 'Awesome Inc' },
playlist: { length: 8 },
},
})
})
})
Expand Down
40 changes: 36 additions & 4 deletions src/__tests__/decide.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { autocapture } from '../autocapture'
import { Decide } from '../decide'
import { _base64Encode } from '../utils'
import { PostHogPersistence } from '../posthog-persistence'

describe('Decide', () => {
given('decide', () => new Decide(given.posthog))
given('posthog', () => ({
get_config: jest.fn().mockImplementation((key) => given.config[key]),
persistence: new PostHogPersistence(given.config),
register: (props) => given.posthog.persistence.register(props),
unregister: (key) => given.posthog.persistence.unregister(key),
get_property: (key) => given.posthog.persistence.props[key],
capture: jest.fn(),
_captureMetrics: { incr: jest.fn() },
_addCaptureHook: jest.fn(),
Expand All @@ -30,7 +35,7 @@ describe('Decide', () => {

given('decideResponse', () => ({ enable_collect_everything: true }))

given('config', () => ({ api_host: 'https://test.com' }))
given('config', () => ({ api_host: 'https://test.com', persistence: 'memory' }))

beforeEach(() => {
jest.spyOn(autocapture, 'afterDecideResponse').mockImplementation()
Expand All @@ -42,6 +47,7 @@ describe('Decide', () => {
given('config', () => ({
api_host: 'https://test.com',
token: 'testtoken',
persistence: 'memory',
}))

it('should call instance._send_request on constructor', () => {
Expand All @@ -63,6 +69,32 @@ describe('Decide', () => {
expect.any(Function)
)
})

it('should send all stored properties with decide request', () => {
given.posthog.register({
$stored_person_properties: { key: 'value' },
$stored_group_properties: { organization: { orgName: 'orgValue' } },
})
given.subject()

expect(given.posthog._send_request).toHaveBeenCalledWith(
'https://test.com/decide/?v=3',
{
data: _base64Encode(
JSON.stringify({
token: 'testtoken',
distinct_id: 'distinctid',
groups: { organization: '5' },
person_properties: { key: 'value' },
group_properties: { organization: { orgName: 'orgValue' } },
})
),
verbose: true,
},
{ method: 'POST' },
expect.any(Function)
)
})
})

describe('parseDecideResponse', () => {
Expand Down Expand Up @@ -97,7 +129,7 @@ describe('Decide', () => {
})

it('does not enable compression from decide response if compression is disabled', () => {
given('config', () => ({ disable_compression: true }))
given('config', () => ({ disable_compression: true, persistence: 'memory' }))
given('decideResponse', () => ({ supportedCompression: ['gzip', 'lz64'] }))
given.subject()

Expand All @@ -115,15 +147,15 @@ describe('Decide', () => {
})

it('runs site apps if opted in', () => {
given('config', () => ({ api_host: 'https://test.com', opt_in_site_apps: true }))
given('config', () => ({ api_host: 'https://test.com', opt_in_site_apps: true, persistence: 'memory' }))
given('decideResponse', () => ({ siteApps: [{ id: 1, url: '/site_app/1/tokentoken/hash/' }] }))
given.subject()
const element = window.document.body.children[0]
expect(element.src).toBe('https://test.com/site_app/1/tokentoken/hash/')
})

it('does not run site apps code if not opted in', () => {
given('config', () => ({ api_host: 'https://test.com', opt_in_site_apps: false }))
given('config', () => ({ api_host: 'https://test.com', opt_in_site_apps: false, persistence: 'memory' }))
given('decideResponse', () => ({ siteApps: [{ id: 1, url: '/site_app/1/tokentoken/hash/' }] }))
expect(() => {
given.subject()
Expand Down
3 changes: 3 additions & 0 deletions src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { autocapture } from './autocapture'
import { _base64Encode, loadScript } from './utils'
import { PostHog } from './posthog-core'
import { Compression, DecideResponse } from './types'
import { STORED_GROUP_PROPERTIES_KEY, STORED_PERSON_PROPERTIES_KEY } from './posthog-persistence'

export class Decide {
instance: PostHog
Expand All @@ -20,6 +21,8 @@ export class Decide {
token: this.instance.get_config('token'),
distinct_id: this.instance.get_distinct_id(),
groups: this.instance.getGroups(),
person_properties: this.instance.get_property(STORED_PERSON_PROPERTIES_KEY),
group_properties: this.instance.get_property(STORED_GROUP_PROPERTIES_KEY),
})

const encoded_data = _base64Encode(json_data)
Expand Down

0 comments on commit bb057aa

Please sign in to comment.