Skip to content

Commit

Permalink
fix: clear flagCallReported if there are new flags (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Oct 11, 2024
1 parent ca271b8 commit 95fe45d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ export abstract class PostHogCore extends PostHogCoreStateless {
private clearProps(): void {
this.props = undefined
this.sessionProps = {}
this.flagCallReported = {}
}

private _props: PostHogEventProperties | undefined
Expand Down Expand Up @@ -1109,6 +1110,11 @@ export abstract class PostHogCore extends PostHogCoreStateless {

return super.getDecide(distinctId, groups, personProperties, groupProperties, extraProperties).then((res) => {
if (res?.featureFlags) {
// clear flag call reported if we have new flags since they might have changed
if (this.sendFeatureFlagEvent) {
this.flagCallReported = {}
}

let newFeatureFlags = res.featureFlags
let newFeatureFlagPayloads = res.featureFlagPayloads
if (res.errorsWhileComputingFlags) {
Expand Down
42 changes: 42 additions & 0 deletions posthog-core/test/posthog.featureflags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,48 @@ describe('PostHog Core', () => {
expect(mocks.fetch).toHaveBeenCalledTimes(2)
})

it('should capture $feature_flag_called again if new flags', async () => {
expect(posthog.getFeatureFlag('feature-1')).toEqual(true)
await waitForPromises()
expect(mocks.fetch).toHaveBeenCalledTimes(2)

expect(parseBody(mocks.fetch.mock.calls[1])).toMatchObject({
batch: [
{
event: '$feature_flag_called',
distinct_id: posthog.getDistinctId(),
properties: {
$feature_flag: 'feature-1',
$feature_flag_response: true,
'$feature/feature-1': true,
},
type: 'capture',
},
],
})

await posthog.reloadFeatureFlagsAsync()
posthog.getFeatureFlag('feature-1')

await waitForPromises()
expect(mocks.fetch).toHaveBeenCalledTimes(4)

expect(parseBody(mocks.fetch.mock.calls[3])).toMatchObject({
batch: [
{
event: '$feature_flag_called',
distinct_id: posthog.getDistinctId(),
properties: {
$feature_flag: 'feature-1',
$feature_flag_response: true,
'$feature/feature-1': true,
},
type: 'capture',
},
],
})
})

it('should persist feature flags', () => {
expect(posthog.getPersistedProperty(PostHogPersistedProperty.FeatureFlags)).toEqual(createMockFeatureFlags())
})
Expand Down
6 changes: 6 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Next

# 3.3.2 - 2024-10-11

## Changed

1. fix: clear flagCallReported if there are new flags

# 3.3.1 - 2024-09-30

## Changed
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthog-react-native",
"version": "3.3.1",
"version": "3.3.2",
"main": "lib/posthog-react-native/index.js",
"files": [
"lib/"
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/src/optional/OptionalSessionReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ try {
web: undefined,
default: require('posthog-react-native-session-replay'), // Only Android and iOS
})
} catch (e) { }
} catch (e) {}

0 comments on commit 95fe45d

Please sign in to comment.