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

feat: Added client side capture rate limiting #1051

Merged
merged 74 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
84b7262
fix(flags): Add a shorter configurable timeout for flag requests
neilkakkar Feb 29, 2024
98d1221
disable retries as well
neilkakkar Mar 4, 2024
a9543d5
Added capture rate limiting
benjackwhite Mar 4, 2024
dab2c0b
Fixes
benjackwhite Mar 4, 2024
5b566d6
Persist rate limiting
benjackwhite Mar 4, 2024
43ac0e3
add default
neilkakkar Mar 4, 2024
977763b
Fix
benjackwhite Mar 5, 2024
c26dd05
Tidying
benjackwhite Mar 5, 2024
11e688f
Fix tests
benjackwhite Mar 5, 2024
2508320
started refacror
benjackwhite Mar 5, 2024
a7eb3e5
wip
benjackwhite Mar 5, 2024
807437a
Removed prepare callback
benjackwhite Mar 5, 2024
4b29011
Removed jsc callback stuff
benjackwhite Mar 5, 2024
7e96981
wip
benjackwhite Mar 5, 2024
c0d1eaa
Fix
benjackwhite Mar 5, 2024
113822f
More fixes
benjackwhite Mar 5, 2024
6f40b29
More fixes
benjackwhite Mar 5, 2024
60faa4c
Fixes all round
benjackwhite Mar 5, 2024
115e84e
Merge branch 'fix/remove-jsc' into feat/request-refactor
benjackwhite Mar 5, 2024
ec2259b
Fixes all round
benjackwhite Mar 5, 2024
d709ed5
Fixes
benjackwhite Mar 5, 2024
f9322af
Added error callback
benjackwhite Mar 5, 2024
63bcc27
Fixes
benjackwhite Mar 5, 2024
c6f933f
Fixes
benjackwhite Mar 5, 2024
53b5ab9
Fixes
benjackwhite Mar 6, 2024
ab9813d
Fixed tests
benjackwhite Mar 6, 2024
6b23ccb
Fixes
benjackwhite Mar 6, 2024
43e5af5
Fix
benjackwhite Mar 6, 2024
b39976b
Fixes
benjackwhite Mar 6, 2024
12f97e6
Fix tests
benjackwhite Mar 6, 2024
d167e54
Fixes
benjackwhite Mar 6, 2024
bb16704
Fix tests
benjackwhite Mar 6, 2024
c8c2674
Fixes
benjackwhite Mar 6, 2024
728856a
Fixed remaining tests
benjackwhite Mar 6, 2024
46b3f49
Fix
benjackwhite Mar 6, 2024
2efecde
More refactoring
benjackwhite Mar 6, 2024
b18ad61
Fixed up tests
benjackwhite Mar 6, 2024
d9710a4
Fix
benjackwhite Mar 6, 2024
8a0348c
Fix
benjackwhite Mar 6, 2024
3487748
Fixes
benjackwhite Mar 6, 2024
15c6a80
Fix cypress tests
benjackwhite Mar 6, 2024
c230a72
Fix unit tests
benjackwhite Mar 6, 2024
cbd547b
Fixes
benjackwhite Mar 6, 2024
5ba60bf
Fixes
benjackwhite Mar 6, 2024
f3f7e22
Update src/types.ts
benjackwhite Mar 6, 2024
3715677
Fixes
benjackwhite Mar 6, 2024
b0efdd5
Merge branch 'feat/request-refactor' of github.com:PostHog/posthog-js…
benjackwhite Mar 6, 2024
2a1b743
Fix
benjackwhite Mar 6, 2024
5ce8b7a
Merge branch 'main' into feat/request-refactor
benjackwhite Mar 6, 2024
11a1f8b
Fixes
benjackwhite Mar 6, 2024
52a7f53
Fix
benjackwhite Mar 6, 2024
e656a33
Fix tests
benjackwhite Mar 6, 2024
8d5997c
Fix
benjackwhite Mar 6, 2024
807b162
Fix
benjackwhite Mar 6, 2024
7bb4fb7
Fix
benjackwhite Mar 6, 2024
0d057f5
Added tests for retries
benjackwhite Mar 6, 2024
d130c97
Fix
benjackwhite Mar 6, 2024
88409b4
Fix comments
benjackwhite Mar 6, 2024
c33f056
Fix
benjackwhite Mar 6, 2024
a52791b
Added abort controller
benjackwhite Mar 7, 2024
32b3894
Merge branch 'feat/request-refactor' into feat/ratelimiting
benjackwhite Mar 7, 2024
dd8f9d5
Fix tests
benjackwhite Mar 7, 2024
096f10b
Fix
benjackwhite Mar 7, 2024
484fe78
Fix
benjackwhite Mar 7, 2024
caede40
Rename
benjackwhite Mar 7, 2024
4e583a3
Merge branch 'main' into feat/ratelimiting
benjackwhite Mar 11, 2024
b92fb92
Fixes
benjackwhite Mar 18, 2024
2abaae3
Merge branch 'main' into feat/ratelimiting
benjackwhite Mar 18, 2024
486b673
Fixed tests
benjackwhite Apr 8, 2024
fc56e90
Merge branch 'main' into feat/ratelimiting
benjackwhite Apr 8, 2024
e8630a8
Fixes
benjackwhite Apr 8, 2024
3c7b925
Fix
benjackwhite Apr 8, 2024
a8bd119
Merge branch 'main' into feat/ratelimiting
benjackwhite Apr 16, 2024
0c49079
Merge branch 'main' into feat/ratelimiting
benjackwhite Apr 17, 2024
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
17 changes: 9 additions & 8 deletions src/__tests__/posthog-core.identify.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { USER_STATE } from '../constants'
import _posthog from '../loader-module'
import { PostHogPersistence } from '../posthog-persistence'
import { uuidv7 } from '../uuidv7'
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('identify()', () => {
given('deviceId', () => given.oldIdentity)

beforeEach(() => {
given.lib.persistence.set_user_state('anonymous')
given.lib.persistence.set_property(USER_STATE, 'anonymous')
})

it('registers new user id and updates alias', () => {
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('identify()', () => {

given.subject()

expect(given.lib.persistence.get_user_state()).toEqual('identified')
expect(given.lib.persistence.get_property(USER_STATE)).toEqual('identified')
})

it('calls capture when there is no device id', () => {
Expand All @@ -101,7 +102,7 @@ describe('identify()', () => {
})

it('calls capture when there is no device id (on first check) even if user is not set to anonymous', () => {
given.lib.persistence.set_user_state(undefined)
given.lib.persistence.set_property(USER_STATE, undefined)
given('oldIdentity', () => 'oldIdentity')
// if null deviceId is set inside identify, but given doesn't reflect that change so....
let wasCalled = false
Expand Down Expand Up @@ -133,7 +134,7 @@ describe('identify()', () => {
*/
given('deviceId', () => 'not the oldIdentity')
// now this is set explicitly by identify
given.lib.persistence.set_user_state('identified')
given.lib.persistence.set_property(USER_STATE, 'identified')

given('identity', () => 'a-new-id')
given('oldIdentity', () => 'oldIdentity')
Expand All @@ -152,7 +153,7 @@ describe('identify()', () => {
given('oldIdentity', () => 'oldIdentity')
given('deviceId', () => 'not the oldIdentity')

given.lib.persistence.set_user_state('identified')
given.lib.persistence.set_property(USER_STATE, 'identified')

given.subject()

Expand All @@ -164,7 +165,7 @@ describe('identify()', () => {
given('identity', () => 'a-new-id')
given('oldIdentity', () => 'oldIdentity')
given('deviceId', () => 'not the oldIdentity')
given.lib.persistence.set_user_state('anonymous')
given.lib.persistence.set_property(USER_STATE, 'anonymous')

given.subject()

Expand Down Expand Up @@ -363,11 +364,11 @@ describe('reset()', () => {
})

it('sets the user as anonymous', () => {
given.lib.persistence.set_user_state('identified')
given.lib.persistence.set_property(USER_STATE, 'identified')

given.subject()

expect(given.lib.persistence.get_user_state()).toEqual('anonymous')
expect(given.lib.persistence.get_property(USER_STATE)).toEqual('anonymous')
})

it('does not reset the device id', () => {
Expand Down
16 changes: 9 additions & 7 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { _info } from '../utils/event-utils'
import { document, window } from '../utils/globals'
import { uuidv7 } from '../uuidv7'
import * as globals from '../utils/globals'
import { USER_STATE } from '../constants'

jest.mock('../gdpr-utils', () => ({
...jest.requireActual('../gdpr-utils'),
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('posthog core', () => {
Object.assign(this.props, properties)
},
props: {},
get_user_state: () => 'anonymous',
get_property: () => 'anonymous',
set_initial_campaign_params: jest.fn(),
set_initial_referrer_info: jest.fn(),
get_initial_props: () => ({}),
Expand All @@ -70,13 +71,14 @@ describe('posthog core', () => {
update_referrer_info: jest.fn(),
update_config: jest.fn(),
properties: jest.fn(),
get_user_state: () => 'anonymous',
get_property: () => 'anonymous',
},
_send_request: jest.fn(),
compression: {},
__captureHooks: [],
rateLimiter: {
isRateLimited: () => false,
isServerRateLimited: () => false,
isCaptureClientSideRateLimited: () => false,
},
}))

Expand Down Expand Up @@ -368,11 +370,11 @@ describe('posthog core', () => {
persistence: {
properties: () => ({ distinct_id: 'abc', persistent: 'prop', $is_identified: false }),
remove_event_timer: jest.fn(),
get_user_state: () => 'anonymous',
get_property: () => 'anonymous',
},
sessionPersistence: {
properties: () => ({ distinct_id: 'abc', persistent: 'prop' }),
get_user_state: () => 'anonymous',
get_property: () => 'anonymous',
},
sessionManager: {
checkAndGetSessionAndWindowId: jest.fn().mockReturnValue({
Expand Down Expand Up @@ -581,7 +583,7 @@ describe('posthog core', () => {

expect(given.lib.get_distinct_id()).toBe('abcd')
expect(given.lib.get_property('$device_id')).toBe('abcd')
expect(given.lib.persistence.get_user_state()).toBe('anonymous')
expect(given.lib.persistence.get_property(USER_STATE)).toBe('anonymous')

given.lib.identify('efgh')

Expand All @@ -606,7 +608,7 @@ describe('posthog core', () => {

expect(given.lib.get_distinct_id()).toBe('abcd')
expect(given.lib.get_property('$device_id')).toBe('og-device-id')
expect(given.lib.persistence.get_user_state()).toBe('identified')
expect(given.lib.persistence.get_property(USER_STATE)).toBe('identified')

given.lib.identify('efgh')
expect(given.overrides.capture).not.toHaveBeenCalled()
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/posthog-persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ describe('persistence', () => {

it('should save user state', () => {
const lib = new PostHogPersistence(makePostHogConfig('bla', persistenceMode))
lib.set_user_state('identified')
lib.set_property(USER_STATE, 'identified')
expect(lib.props[USER_STATE]).toEqual('identified')
})

it('can load user state', () => {
const lib = new PostHogPersistence(makePostHogConfig('bla', persistenceMode))
lib.set_user_state('identified')
expect(lib.get_user_state()).toEqual('identified')
lib.set_property(USER_STATE, 'identified')
expect(lib.get_property(USER_STATE)).toEqual('identified')
})

it('has user state as a reserved property key', () => {
const lib = new PostHogPersistence(makePostHogConfig('bla', persistenceMode))
lib.register({ distinct_id: 'testy', test_prop: 'test_value' })
lib.set_user_state('identified')
lib.set_property(USER_STATE, 'identified')
expect(lib.properties()).toEqual({ distinct_id: 'testy', test_prop: 'test_value' })
})

Expand Down
Loading
Loading