-
Notifications
You must be signed in to change notification settings - Fork 305
[TikTok Audiences] - new syncAudience action #3732
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
Open
joe-ayoub-segment
wants to merge
20
commits into
main
Choose a base branch
from
tt-syncaudience
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
856d419
[TikTok Audiences] - new syncAudience action
joe-ayoub-segment 4ab806e
progress
joe-ayoub-segment 5df24d6
savng progress
joe-ayoub-segment 6922301
progress
joe-ayoub-segment ec01486
sd
joe-ayoub-segment c99eeb2
continuing
joe-ayoub-segment 199420c
fixing error handling
joe-ayoub-segment 59494bb
fixing error handling
joe-ayoub-segment 9121d64
adding test coverage
joe-ayoub-segment 266a2d0
adding new preset
joe-ayoub-segment 9574669
removing unused flag
joe-ayoub-segment 96b324f
removing preset
joe-ayoub-segment 9c81251
Copilot issues addressed
joe-ayoub-segment 95a5ae7
more copilor comments
joe-ayoub-segment 72a9050
yarn types
joe-ayoub-segment 919b932
error handling change
joe-ayoub-segment 2472645
reducing number of batch keys
joe-ayoub-segment 71ae8c8
adding instrumentation
joe-ayoub-segment 1cdb284
Merge branch 'main' into tt-syncaudience
joe-ayoub-segment fd601a6
copilot comment fix
joe-ayoub-segment File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
...estination-actions/src/destinations/tiktok-audiences/syncAudience/__tests__/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| import nock from 'nock' | ||
| import { createTestEvent, createTestIntegration } from '@segment/actions-core' | ||
| import Destination from '../../index' | ||
| import { BASE_URL } from '../../constants' | ||
| import { TIKTOK_AUDIENCES_API_VERSION } from '../../versioning-info' | ||
|
|
||
| const testDestination = createTestIntegration(Destination) | ||
|
|
||
| interface AuthTokens { | ||
| accessToken: string | ||
| refreshToken: string | ||
| } | ||
|
|
||
| const auth: AuthTokens = { | ||
| accessToken: 'test', | ||
| refreshToken: 'test' | ||
| } | ||
|
|
||
| const EXTERNAL_AUDIENCE_ID = '12345' | ||
| const ADVERTISER_ID = '123' // References audienceSettings.advertiserId | ||
| const ADVERTISING_ID = '4242' // References device.advertisingId | ||
| const ID_TYPE = 'EMAIL_SHA256' // References audienceSettings.idType | ||
|
|
||
| const event = createTestEvent({ | ||
| event: 'Audience Entered', | ||
| type: 'track', | ||
| properties: {}, | ||
| context: { | ||
| device: { | ||
| advertisingId: ADVERTISING_ID | ||
| }, | ||
| traits: { | ||
| email: '[email protected]', | ||
| phone: '1234567890' | ||
| }, | ||
| personas: { | ||
| audience_settings: { | ||
| advertiserId: ADVERTISER_ID, | ||
| idType: ID_TYPE | ||
| }, | ||
| external_audience_id: EXTERNAL_AUDIENCE_ID | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| const updateUsersRequestBody = { | ||
| id_schema: ['EMAIL_SHA256', 'PHONE_SHA256', 'IDFA_SHA256'], | ||
| advertiser_ids: [ADVERTISER_ID], | ||
| action: 'add', | ||
| batch_data: [ | ||
| [ | ||
| { | ||
| id: '584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777', | ||
| audience_ids: [EXTERNAL_AUDIENCE_ID] | ||
| }, | ||
| { | ||
| id: 'c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646', | ||
| audience_ids: [EXTERNAL_AUDIENCE_ID] | ||
| }, | ||
| { | ||
| id: '0315b4020af3eccab7706679580ac87a710d82970733b8719e70af9b57e7b9e6', | ||
| audience_ids: [EXTERNAL_AUDIENCE_ID] | ||
| } | ||
| ] | ||
| ] | ||
| } | ||
|
|
||
| describe('TiktokAudiences.addToAudience', () => { | ||
| it('should succeed if audience id is valid', async () => { | ||
| nock(`${BASE_URL}${TIKTOK_AUDIENCES_API_VERSION}`).post('/segment/mapping/', updateUsersRequestBody).reply(200) | ||
|
|
||
| const r = await testDestination.testAction('addToAudience', { | ||
| auth, | ||
|
joe-ayoub-segment marked this conversation as resolved.
Outdated
|
||
| event, | ||
| settings: {}, | ||
| useDefaultMappings: true, | ||
|
joe-ayoub-segment marked this conversation as resolved.
Outdated
|
||
| mapping: { | ||
| send_advertising_id: true | ||
| } | ||
| }) | ||
|
|
||
| expect(r[0].status).toEqual(200) | ||
| expect(r[0].options.body).toMatchInlineSnapshot( | ||
| `"{\\"advertiser_ids\\":[\\"123\\"],\\"action\\":\\"add\\",\\"id_schema\\":[\\"EMAIL_SHA256\\",\\"PHONE_SHA256\\",\\"IDFA_SHA256\\"],\\"batch_data\\":[[{\\"id\\":\\"584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777\\",\\"audience_ids\\":[\\"12345\\"]},{\\"id\\":\\"c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646\\",\\"audience_ids\\":[\\"12345\\"]},{\\"id\\":\\"0315b4020af3eccab7706679580ac87a710d82970733b8719e70af9b57e7b9e6\\",\\"audience_ids\\":[\\"12345\\"]}]]}"` | ||
| ) | ||
| }) | ||
|
|
||
| it('should normalize and hash emails correctly', async () => { | ||
| nock(`${BASE_URL}${TIKTOK_AUDIENCES_API_VERSION}`) | ||
| .post('/segment/mapping/', { | ||
| advertiser_ids: ['123'], | ||
| action: 'add', | ||
| id_schema: ['EMAIL_SHA256'], | ||
| batch_data: [ | ||
| [ | ||
| { | ||
| id: '584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777', | ||
| audience_ids: [EXTERNAL_AUDIENCE_ID] | ||
| } | ||
| ] | ||
| ] | ||
| }) | ||
| .reply(200) | ||
|
|
||
| const responses = await testDestination.testAction('addToAudience', { | ||
| event, | ||
| settings: { | ||
| advertiser_ids: ['123'] | ||
| }, | ||
| useDefaultMappings: true, | ||
| auth, | ||
| mapping: { | ||
| send_advertising_id: false, | ||
| send_phone: false | ||
| } | ||
| }) | ||
|
|
||
| expect(responses[0].options.body).toMatchInlineSnapshot( | ||
| `"{\\"advertiser_ids\\":[\\"123\\"],\\"action\\":\\"add\\",\\"id_schema\\":[\\"EMAIL_SHA256\\"],\\"batch_data\\":[[{\\"id\\":\\"584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777\\",\\"audience_ids\\":[\\"12345\\"]}]]}"` | ||
| ) | ||
| }) | ||
|
|
||
| it('should normalize and hash phone correctly', async () => { | ||
| nock(`${BASE_URL}${TIKTOK_AUDIENCES_API_VERSION}`) | ||
| .post('/segment/mapping/', { | ||
| advertiser_ids: ['123'], | ||
| action: 'add', | ||
| id_schema: ['PHONE_SHA256'], | ||
| batch_data: [ | ||
| [ | ||
| { | ||
| id: 'c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646', | ||
| audience_ids: [EXTERNAL_AUDIENCE_ID] | ||
| } | ||
| ] | ||
| ] | ||
| }) | ||
| .reply(200) | ||
|
|
||
| const responses = await testDestination.testAction('addToAudience', { | ||
| event, | ||
| settings: { | ||
| advertiser_ids: ['123'] | ||
| }, | ||
| useDefaultMappings: true, | ||
| auth, | ||
| mapping: { | ||
| send_advertising_id: false, | ||
| send_email: false | ||
| } | ||
| }) | ||
|
|
||
| expect(responses[0].options.body).toMatchInlineSnapshot( | ||
| `"{\\"advertiser_ids\\":[\\"123\\"],\\"action\\":\\"add\\",\\"id_schema\\":[\\"PHONE_SHA256\\"],\\"batch_data\\":[[{\\"id\\":\\"c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646\\",\\"audience_ids\\":[\\"12345\\"]}]]}"` | ||
| ) | ||
| }) | ||
|
|
||
| it('should fail if an audience id is invalid', async () => { | ||
| const anotherEvent = createTestEvent({ | ||
| event: 'Audience Entered', | ||
| type: 'track', | ||
| properties: { | ||
| audience_key: 'personas_test_audience' | ||
| }, | ||
| context: { | ||
| device: { | ||
| advertisingId: ADVERTISING_ID | ||
| }, | ||
| traits: { | ||
| email: '[email protected]', | ||
| phone: '1234567890' | ||
| }, | ||
| personas: { | ||
| audience_settings: { | ||
| advertiserId: ADVERTISER_ID, | ||
| idType: ID_TYPE | ||
| }, | ||
| external_audience_id: 'THIS_ISNT_REAL' | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| nock(`${BASE_URL}${TIKTOK_AUDIENCES_API_VERSION}/segment/mapping/`) | ||
| .post(/.*/, { | ||
| id_schema: ['EMAIL_SHA256', 'PHONE_SHA256', 'IDFA_SHA256'], | ||
| advertiser_ids: [ADVERTISER_ID], | ||
| action: 'add', | ||
| batch_data: [ | ||
| [ | ||
| { | ||
| id: '584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777', | ||
| audience_ids: ['THIS_ISNT_REAL'] | ||
| }, | ||
| { | ||
| id: 'c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646', | ||
| audience_ids: ['THIS_ISNT_REAL'] | ||
| }, | ||
| { | ||
| id: '0315b4020af3eccab7706679580ac87a710d82970733b8719e70af9b57e7b9e6', | ||
| audience_ids: ['THIS_ISNT_REAL'] | ||
| } | ||
| ] | ||
| ] | ||
| }) | ||
| .reply(400) | ||
|
|
||
| const r = await testDestination.testAction('addToAudience', { | ||
| event: anotherEvent, | ||
| settings: { | ||
| advertiser_ids: ['123'] | ||
| }, | ||
| useDefaultMappings: true, | ||
| auth, | ||
| mapping: {} | ||
| }) | ||
|
|
||
| expect(r[0].status).toEqual(400) | ||
| }) | ||
|
|
||
| it('should fail if all the send fields are false', async () => { | ||
| nock(`${BASE_URL}${TIKTOK_AUDIENCES_API_VERSION}/segment/mapping/`).post(/.*/, updateUsersRequestBody).reply(200) | ||
|
|
||
| await expect( | ||
| testDestination.testAction('addToAudience', { | ||
| event, | ||
| settings: { | ||
| advertiser_ids: ['123'] | ||
| }, | ||
| useDefaultMappings: true, | ||
| auth, | ||
| mapping: { | ||
| selected_advertiser_id: '123', | ||
| audience_id: '123456', | ||
| send_email: false, | ||
| send_advertising_id: false, | ||
| send_phone: false | ||
| } | ||
| }) | ||
| ).rejects.toThrow('At least one of `Send Email`, `Send Phone` or `Send Advertising ID` must be set to `true`.') | ||
| }) | ||
| it('should fail if email and/or advertising_id is not in the payload', async () => { | ||
| nock(`${BASE_URL}${TIKTOK_AUDIENCES_API_VERSION}/segment/mapping/`).post(/.*/, updateUsersRequestBody).reply(400) | ||
|
|
||
| delete event?.context?.device | ||
| delete event?.context?.traits | ||
|
|
||
| await expect( | ||
| testDestination.testAction('addToAudience', { | ||
| event, | ||
| settings: { | ||
| advertiser_ids: ['123'] | ||
| }, | ||
| useDefaultMappings: true, | ||
| auth, | ||
| mapping: { | ||
| send_email: true, | ||
| send_advertising_id: true | ||
| } | ||
| }) | ||
| ).rejects.toThrowError('At least one of Email Id or Advertising ID must be provided.') | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.