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 Mixpanel distinct_id for anonymous events #2684

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,36 @@ describe('Mixpanel.trackEvent', () => {
}
})

it('should allow an empty, but present, distinct_id', async () => {
const event = createTestEvent({ timestamp, event: 'Test Event' })

nock('https://api.mixpanel.com').post('/import?strict=1').reply(200, {})

const responses = await testDestination.testAction('trackEvent', {
event,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET
},
useDefaultMappings: true,
mapping: {
distinct_id: '' // Map an empty distinct_id for the action.
}
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].data).toMatchObject({})
expect(responses[0].options.json).toMatchObject([
{
event: 'Test Event',
properties: expect.objectContaining({
...expectedProperties,
distinct_id: '' // Expect an empty string `distinct_id` returned.
})
}
])
})

it('should invoke performBatch for batches', async () => {
const events = [
createTestEvent({ timestamp, event: 'Test Event1' }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ export function getEventProperties(payload: Payload, settings: Settings): Mixpan
browserVersion = getBrowserVersion(payload.userAgent)
}
const integration = payload.context?.integration as Record<string, string>

// When the `distinct_id` property is undefined we add the property in as a blank string,
// so that we can support Mixpanel events that aren't tied to a specific user.
//
// The `distinct_id` property is, according to documentation, supposed to
// ALWAYS be present, either with an identifying value or with an empty
// string value to denote that the event should be excluded from behavioral
// analysis.
//
// @see https://developer.mixpanel.com/reference/import-events#propertiesdistinct_id
const distinct_id = payload.distinct_id ?? ''

return {
time: time,
ip: payload.ip,
id: payload.distinct_id,
$anon_id: payload.anonymous_id,
distinct_id: payload.distinct_id,
distinct_id,
$app_build_number: payload.app_build,
$app_version_string: payload.app_version,
$app_namespace: payload.app_namespace,
Expand Down
Loading