Skip to content

Commit

Permalink
Add warning and conversion for number distinct_id
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Feb 2, 2024
1 parent 54c8a97 commit b59d628
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 26 additions & 1 deletion functional_tests/identify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { waitFor } from '@testing-library/dom'
import { v4 } from 'uuid'
import { getRequests } from './mock-server'
import { createPosthogInstance } from './posthog-instance'

import { logger } from '../src/utils/logger'
jest.mock('../src/utils/logger')
test('identify sends a identify event', async () => {
const token = v4()
const posthog = await createPosthogInstance(token)
Expand All @@ -24,6 +25,30 @@ test('identify sends a identify event', async () => {
})
)
)
expect(jest.mocked(logger).error).toBeCalledTimes(0)
})

test('identify warns and succeed when given a number distinct_id', async () => {
const token = v4()
const posthog = await createPosthogInstance(token)

const distinctIdNum = 123
const distinctIdString = '123'

posthog.identify(distinctIdNum as any)

await waitFor(() =>
expect(getRequests(token)['/e/']).toContainEqual(
expect.objectContaining({
event: '$identify',
properties: expect.objectContaining({
distinct_id: distinctIdString,
token: posthog.config.token,
}),
})
)
)
expect(jest.mocked(logger).error).toBeCalledTimes(1)
})

test('identify sends an engage request if identify called twice with the same distinct id and with $set/$set_once', async () => {
Expand Down
15 changes: 14 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ import { PostHogSurveys } from './posthog-surveys'
import { RateLimiter } from './rate-limiter'
import { uuidv7 } from './uuidv7'
import { SurveyCallback } from './posthog-surveys-types'
import { _isArray, _isEmptyObject, _isFunction, _isObject, _isString, _isUndefined } from './utils/type-utils'
import {
_isArray,
_isEmptyObject,
_isFunction,
_isNumber,
_isObject,
_isString,
_isUndefined,
} from './utils/type-utils'
import { _info } from './utils/event-utils'
import { logger } from './utils/logger'
import { document, userAgent } from './utils/globals'
Expand Down Expand Up @@ -1293,6 +1301,11 @@ export class PostHog {
if (!this.__loaded || !this.persistence) {
return logger.uninitializedWarning('posthog.identify')
}
if (_isNumber(new_distinct_id)) {
logger.error('The first argument to posthog.identify was a number, but it should be a string.')
new_distinct_id = (new_distinct_id as number).toString()
}

//if the new_distinct_id has not been set ignore the identify event
if (!new_distinct_id) {
logger.error('Unique user id has not been set in posthog.identify')
Expand Down

0 comments on commit b59d628

Please sign in to comment.