Skip to content

Commit

Permalink
feat: add new destination tiktok_audience (#2710)
Browse files Browse the repository at this point in the history
* feat: add new destination tiktok_audience

* remove log

* refactor code

* chore: refactor workflows

* add test cases

* chore: refactore code

* refactor code and add negative test case

---------

Co-authored-by: Dilip Kola <[email protected]>
Co-authored-by: mihir-4116 <[email protected]>
Co-authored-by: Mihir Bhalala <[email protected]>
  • Loading branch information
4 people authored Nov 2, 2023
1 parent 57fee04 commit 9bc0fd8
Show file tree
Hide file tree
Showing 7 changed files with 1,802 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/cdk/v2/bindings/default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const crypto = require('crypto');
const {
InstrumentationError,
ConfigurationError,
Expand Down Expand Up @@ -47,7 +48,12 @@ function assertHttpResp(processedResponse, message) {
}
}

function MD5(data) {
return crypto.createHash('md5').update(data).digest('hex');
}

module.exports = {
MD5,
isValidEventType,
assert,
assertConfig,
Expand Down
9 changes: 9 additions & 0 deletions src/cdk/v2/destinations/tiktok_audience/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const ACTION_MAP = {
add: 'add',
remove: 'delete',
};
const SHA256_TRAITS = ['IDFA_SHA256', 'AAID_SHA256', 'EMAIL_SHA256', 'PHONE_SHA256'];
module.exports = {
ACTION_MAP,
SHA256_TRAITS,
};
67 changes: 67 additions & 0 deletions src/cdk/v2/destinations/tiktok_audience/procWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

bindings:
- name: EventType
path: ../../../../constants
- path: ../../bindings/jsontemplate
exportAll: true
- path: ./config
- name: removeUndefinedAndNullValues
path: ../../../../v0/util
- name: defaultRequestConfig
path: ../../../../v0/util

steps:
- name: validateInput
template: |
let messageType = .message.type;
$.assert(.message.type, "message Type is not present. Aborting message.");
$.assert(.message.type.toLowerCase() ==='audiencelist', "Event type " + .message.type.toLowerCase() + " is not supported. Aborting message.");
$.assert(.message.properties, "Message properties is not present. Aborting message.");
$.assert(.message.properties.listData, "listData is not present inside properties. Aborting message.");
$.assert($.containsAll(Object.keys(.message.properties.listData), ["add", "remove"]), "unsupported action type. Aborting message.")
- name: prepareIdentifiersList
description: |
Populate list of identifiers to be updated
template: |
const destinationFields = .message.context.destinationFields.split(", ")
const audienceId = .message.context.externalId[0].type.split("-")[1];
const isHashRequired = .destination.Config.isHashRequired;
const advertiserIds = .metadata.secret.advertiserIds;
const hashTraits = function(traits) {
traits@trait.(destinationFields@destinationField.(
trait[destinationField] ? {
id: isHashRequired ?
destinationField in $.SHA256_TRAITS ?
$.SHA256(trait[destinationField]) : $.MD5(trait[destinationField])
: trait[destinationField],
audience_ids:[audienceId]
} : {}
)[])
};
const listData = .message.properties.listData;
const actions = Object.keys(listData)
actions@action.({
"batch_data": hashTraits(listData[action]),
"id_schema": destinationFields,
"advertiser_ids": advertiserIds,
"action": $.ACTION_MAP[action],
})[]
- name: buildResponseForProcessTransformation
description: build response
template: |
const accessToken = .metadata.secret.accessToken
const anonymousId = .message.anonymousId;
$.outputs.prepareIdentifiersList@body.(
let response = $.defaultRequestConfig();
response.body.JSON = body;
response.userId = anonymousId;
response.endpoint = "https://business-api.tiktok.com/open_api/v1.3/segment/mapping/";
response.headers = {
"Access-Token": accessToken,
"Content-Type": "application/json"
};
response
)
32 changes: 32 additions & 0 deletions src/cdk/v2/destinations/tiktok_audience/rtWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
bindings:
- name: handleRtTfSingleEventError
path: ../../../../v0/util/index

steps:
- name: validateInput
template: |
$.assert(Array.isArray(^) && ^.length > 0, "Invalid event array")
- name: transform
externalWorkflow:
path: ./procWorkflow.yaml
loopOverInput: true

- name: successfulEvents
debug: true
template: |
$.outputs.transform#idx{"output" in .}.({
"batchedRequest": .output,
"batched": true,
"destination": ^[idx].destination,
"metadata": ^[idx].metadata[],
"statusCode": 200
})[]
- name: failedEvents
template: |
$.outputs.transform#idx.error.(
$.handleRtTfSingleEventError(^[idx], .originalError ?? ., {})
)[]
- name: finalPayload
template: |
[...$.outputs.failedEvents, ...$.outputs.successfulEvents]
1 change: 1 addition & 0 deletions src/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"OPTIMIZELY_FULLSTACK": true,
"TWITTER_ADS": true,
"CLEVERTAP": true,
"TIKTOK_AUDIENCE": true,
"ORTTO": true
}
}
Loading

0 comments on commit 9bc0fd8

Please sign in to comment.