Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear flagCallReported if there are new flags #281

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-10
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

## 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
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) {}