Skip to content

Commit

Permalink
Add new India region for mixpanel (#2575)
Browse files Browse the repository at this point in the history
* add new India region for mixpanel

* simplfy consition blocks

* update setting description to include india
  • Loading branch information
tcgilbert authored Dec 10, 2024
1 parent 50509c3 commit 7ba5f75
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ describe('Mixpanel.alias', () => {
)
})

it('should use IN server URL', async () => {
const event = createTestEvent({ previousId: 'test-prev-id' })
nock('https://api-in.mixpanel.com').post('/track').reply(200, {})
const responses = await testDestination.testAction('alias', {
event,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.IN
}
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].data).toMatchObject({})
expect(responses[0].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
event: '$create_alias',
properties: {
distinct_id: 'test-prev-id',
alias: 'user1234',
token: MIXPANEL_PROJECT_TOKEN
}
})
})
)
})

it('should default to US endpoint if apiRegion setting is undefined', async () => {
const event = createTestEvent({ previousId: 'test-prev-id' })

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum ApiRegions {
US = 'US 🇺🇸',
EU = 'EU 🇪🇺'
EU = 'EU 🇪🇺',
IN = 'IN 🇮🇳'
}

export enum StrictMode {
Expand All @@ -16,7 +17,10 @@ export function getApiServerUrl(apiRegion: string | undefined) {
if (apiRegion == ApiRegions.EU) {
return 'https://api-eu.mixpanel.com'
}
return 'https://api.mixpanel.com'
if (apiRegion == ApiRegions.IN) {
return 'https://api-in.mixpanel.com'
}
return 'https://api.mixpanel.com' // Default US endpoint
}

export function getBrowser(userAgent: string): string {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,40 @@ describe('Mixpanel.groupIdentifyUser', () => {
)
})

it('should use IN server URL', async () => {
const event = createTestEvent({
timestamp,
groupId: 'test-group-id',
traits: { hello: 'world', company: 'Mixpanel' }
})
nock('https://api-in.mixpanel.com').post('/groups').reply(200, {})
const responses = await testDestination.testAction('groupIdentifyUser', {
event,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.IN
}
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].data).toMatchObject({})
expect(responses[0].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
$token: MIXPANEL_PROJECT_TOKEN,
$group_key: '$group_id',
$group_id: 'test-group-id',
$set: {
hello: 'world',
company: 'Mixpanel'
}
})
})
)
})

it('should default to US endpoint if apiRegion setting is undefined', async () => {
const event = createTestEvent({
timestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,50 @@ describe('Mixpanel.identifyUser', () => {
)
})

it('should use IN server URL', async () => {
const event = createTestEvent({ timestamp, traits: { abc: '123' } })
nock('https://api-in.mixpanel.com').post('/engage').reply(200, {})
nock('https://api-in.mixpanel.com').post('/track').reply(200, {})
const responses = await testDestination.testAction('identifyUser', {
event,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.IN
}
})
expect(responses.length).toBe(2)
expect(responses[0].status).toBe(200)
expect(responses[0].data).toMatchObject({})
expect(responses[0].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
event: '$identify',
properties: {
$identified_id: 'user1234',
$anon_id: event.anonymousId,
token: MIXPANEL_PROJECT_TOKEN
}
})
})
)
expect(responses[1].status).toBe(200)
expect(responses[1].data).toMatchObject({})
expect(responses[1].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
$token: MIXPANEL_PROJECT_TOKEN,
$distinct_id: 'user1234',
$ip: '8.8.8.8',
$set: {
abc: '123'
}
})
})
)
})

it('should default to US endpoint if apiRegion setting is undefined', async () => {
const event = createTestEvent({ timestamp, traits: { abc: '123' } })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ describe('Mixpanel.incrementProperties', () => {
)
})

it('should use IN server URL', async () => {
const event = createTestEvent({ timestamp, event: 'search', properties: defaultProperties })

nock('https://api-in.mixpanel.com').post('/engage').reply(200, {})
nock('https://api-in.mixpanel.com').post('/track').reply(200, {})

const responses = await testDestination.testAction('incrementProperties', {
event,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.IN
}
})

expect(responses[0].status).toBe(200)
expect(responses[0].data).toMatchObject({})
expect(responses[0].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
$token: MIXPANEL_PROJECT_TOKEN,
$distinct_id: 'user1234',
$ip: '8.8.8.8',
$add: {
searches: 1
}
})
})
)
})

it('should default to US endpoint if apiRegion setting is undefined', async () => {
const event = createTestEvent({ timestamp, event: 'search', properties: defaultProperties })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const destination: DestinationDefinition<Settings> = {
apiRegion: {
label: 'Data Residency',
description:
'Learn about [EU data residency](https://help.mixpanel.com/hc/en-us/articles/360039135652-Data-Residency-in-EU)',
'Learn about [EU data residency](https://docs.mixpanel.com/docs/privacy/eu-residency) and [India data residency](https://docs.mixpanel.com/docs/privacy/in-residency)',
type: 'string',
choices: Object.values(ApiRegions).map((apiRegion) => ({ label: apiRegion, value: apiRegion })),
default: ApiRegions.US
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ describe('Mixpanel.trackEvent', () => {
])
})

it('should use IN server URL', async () => {
const event = createTestEvent({ timestamp, event: 'Test Event' })

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

const responses = await testDestination.testAction('trackEvent', {
event,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.IN
}
})
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)
}
])
})

it('should default to US endpoint if apiRegion setting is undefined', async () => {
const event = createTestEvent({ timestamp, event: 'Test Event' })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ describe('Mixpanel.trackPurchase', () => {
expect(responses[0].status).toBe(200)
})

it('should use IN server URL', async () => {
const event = createTestEvent(orderCompletedEvent)

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

const responses = await testDestination.testAction('trackPurchase', {
event,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.IN
}
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
})

it('should default to US endpoint if apiRegion setting is undefined', async () => {
const event = createTestEvent(orderCompletedEvent)

Expand Down

0 comments on commit 7ba5f75

Please sign in to comment.