Skip to content

Commit bd304d5

Browse files
committed
cleanup for smart hashing
1 parent 95b7875 commit bd304d5

File tree

23 files changed

+153
-162
lines changed

23 files changed

+153
-162
lines changed

packages/destination-actions/src/destinations/delivrai-activate/updateSegment/index.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ActionDefinition, Features, RequestClient } from '@segment/actions-core'
1+
import type { ActionDefinition, RequestClient } from '@segment/actions-core'
22
import { PayloadValidationError, InvalidAuthenticationError, APIError } from '@segment/actions-core'
33
import type { Settings } from '../generated-types'
44
import type { Payload } from './generated-types'
@@ -96,7 +96,7 @@ const action: ActionDefinition<Settings, Payload> = {
9696
}
9797
},
9898

99-
perform: async (request, { payload, settings, features }) => {
99+
perform: async (request, { payload, settings }) => {
100100
// Check if client_identifier_id is valid
101101
let rt_access_token_response
102102
// Generate JWT token
@@ -111,9 +111,9 @@ const action: ActionDefinition<Settings, Payload> = {
111111
}
112112
const rt_access_token = rt_access_token_response?.data?.token
113113
// Process the payload with the valid token
114-
return process_payload(request, [payload], rt_access_token, settings.client_identifier_id, features || {})
114+
return process_payload(request, [payload], rt_access_token, settings.client_identifier_id)
115115
},
116-
performBatch: async (request, { payload, settings, features }) => {
116+
performBatch: async (request, { payload, settings }) => {
117117
// Ensure client_identifier_id is provided
118118
let rt_access_token_response
119119
// Generate JWT token
@@ -128,7 +128,7 @@ const action: ActionDefinition<Settings, Payload> = {
128128
}
129129
const rt_access_token = rt_access_token_response?.data?.token
130130
// Process the payload with the valid token
131-
return process_payload(request, payload, rt_access_token, settings.client_identifier_id, features ?? {})
131+
return process_payload(request, payload, rt_access_token, settings.client_identifier_id)
132132
}
133133
}
134134

@@ -137,10 +137,9 @@ async function process_payload(
137137
request: RequestClient,
138138
payload: Payload[],
139139
token: string | undefined,
140-
client_identifier_id: string,
141-
features: Features
140+
client_identifier_id: string
142141
) {
143-
const body = gen_update_segment_payload(payload, client_identifier_id, features)
142+
const body = gen_update_segment_payload(payload, client_identifier_id)
144143
// Send request to Delivr AI only when all events in the batch include selected Ids
145144
if (body.data.length > 0) {
146145
return await request(`${DELIVRAI_BASE_URL}${DELIVRAI_SEGMENTATION_AUDIENCE}`, {

packages/destination-actions/src/destinations/delivrai-activate/utils-rt.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Payload } from './updateSegment/generated-types'
22
import { DelivrAIPayload } from './types'
3-
import { Features, RequestClient } from '@segment/actions-core'
3+
import { RequestClient } from '@segment/actions-core'
44
import { DELIVRAI_BASE_URL, DELIVRAI_GET_TOKEN } from './constants'
5-
import { processHashing } from '../../lib/hashing-utils'
5+
import { processHashingV2 } from '../../lib/hashing-utils'
66

77
/**
88
* Generates JWT for Realtime API authentication
@@ -50,11 +50,7 @@ export function validate_phone(phone: string) {
5050
return ''
5151
}
5252
}
53-
export function gen_update_segment_payload(
54-
payloads: Payload[],
55-
client_identifier_id: string,
56-
features: Features
57-
): DelivrAIPayload {
53+
export function gen_update_segment_payload(payloads: Payload[], client_identifier_id: string): DelivrAIPayload {
5854
const data_groups: {
5955
[hashed_email: string]: {
6056
exp: string
@@ -68,7 +64,7 @@ export function gen_update_segment_payload(
6864
for (const event of payloads) {
6965
let hashed_email: string | undefined = ''
7066
if (event.email) {
71-
hashed_email = processHashing(event.email.toLowerCase(), 'sha256', 'hex', features, 'actions-delivrai-audiences')
67+
hashed_email = processHashingV2(event.email.toLowerCase(), 'sha256', 'hex')
7268
}
7369
let idfa: string | undefined = ''
7470
let gpsaid: string | undefined = ''

packages/destination-actions/src/destinations/dynamic-yield-audiences/helpers.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Features } from '@segment/actions-core'
2-
import { processHashing } from '../../lib/hashing-utils'
1+
import { processHashingV2 } from '../../lib/hashing-utils'
32

4-
export function hashAndEncode(property: string, features: Features): string {
5-
return processHashing(property, 'sha256', 'hex', features, 'actions-dynamic-yield-audiences')
3+
export function hashAndEncode(property: string): string {
4+
return processHashingV2(property, 'sha256', 'hex')
65
}
76

8-
export function hashAndEncodeToInt(property: string, features: Features): number {
9-
const hash = processHashing(property, 'sha256', 'hex', features, 'actions-dynamic-yield-audiences')
7+
export function hashAndEncodeToInt(property: string): number {
8+
const hash = processHashingV2(property, 'sha256', 'hex')
109
const bigInt = BigInt('0x' + hash)
1110
let integerString = bigInt.toString()
1211
if (integerString.length > 16) {

packages/destination-actions/src/destinations/dynamic-yield-audiences/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
9090
throw new IntegrationError('Missing computation parameters: Id and Key', 'MISSING_REQUIRED_FIELD', 400)
9191
}
9292

93-
const audience_id = hashAndEncodeToInt(personas.computation_id, createAudienceInput.features || {})
93+
const audience_id = hashAndEncodeToInt(personas.computation_id)
9494

9595
const json = {
9696
type: 'audience_subscription_request',

packages/destination-actions/src/destinations/dynamic-yield-audiences/syncAudience/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
110110
}
111111
},
112112

113-
perform: async (request, { audienceSettings, payload, settings, features }) => {
113+
perform: async (request, { audienceSettings, payload, settings }) => {
114114
const { external_audience_id } = payload
115115

116116
if (!audienceSettings?.audience_name) {
@@ -177,7 +177,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
177177
{
178178
type: idTypeToSend,
179179
encoding: idTypeToSend === 'email' ? '"sha-256"' : 'raw',
180-
value: idTypeToSend === 'email' ? hashAndEncode(primaryIdentifier, features || {}) : primaryIdentifier
180+
value: idTypeToSend === 'email' ? hashAndEncode(primaryIdentifier) : primaryIdentifier
181181
}
182182
],
183183
audiences: [

packages/destination-actions/src/destinations/reddit-conversions-api/__tests__/index.test.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('Reddit Conversions Api', () => {
6060
click_id: 'click_id_1',
6161
event_at: '2024-01-08T13:52:50.212Z',
6262
event_metadata: {
63-
conversion_id: '492ebaa71872336ef94c7093b77d2232fdba7e469f716586a816d861367b183f',
63+
conversion_id: 'ea3d01f99e303d2338cfb4e71f182441eb57c9a3cb129c40bcae9f5d641a7375',
6464
currency: 'USD',
6565
item_count: 10,
6666
products: [
@@ -131,8 +131,7 @@ describe('Reddit Conversions Api', () => {
131131
useDefaultMappings: true,
132132
mapping: {
133133
custom_event_name: 'Some Custom Event Name'
134-
},
135-
features: { 'smart-hashing': true }
134+
}
136135
})
137136

138137
expect(responses.length).toBe(1)
@@ -227,7 +226,7 @@ describe('Reddit Conversions Api', () => {
227226
click_id: 'click_id_1',
228227
event_at: '2024-01-08T13:52:50.212Z',
229228
event_metadata: {
230-
conversion_id: '492ebaa71872336ef94c7093b77d2232fdba7e469f716586a816d861367b183f',
229+
conversion_id: 'ea3d01f99e303d2338cfb4e71f182441eb57c9a3cb129c40bcae9f5d641a7375',
231230
currency: 'USD',
232231
item_count: 10,
233232
products: [
@@ -307,7 +306,7 @@ describe('Reddit Conversions Api', () => {
307306
click_id: 'click_id_1',
308307
event_at: '2024-01-08T13:52:50.212Z',
309308
event_metadata: {
310-
conversion_id: '492ebaa71872336ef94c7093b77d2232fdba7e469f716586a816d861367b183f',
309+
conversion_id: 'ea3d01f99e303d2338cfb4e71f182441eb57c9a3cb129c40bcae9f5d641a7375',
311310
currency: 'USD',
312311
products: [
313312
{
@@ -375,8 +374,7 @@ describe('Reddit Conversions Api', () => {
375374
useDefaultMappings: true,
376375
mapping: {
377376
tracking_type: 'Lead'
378-
},
379-
features: { 'smart-hashing': true }
377+
}
380378
})
381379

382380
expect(responses.length).toBe(1)
@@ -453,8 +451,7 @@ describe('Reddit Conversions Api', () => {
453451
useDefaultMappings: true,
454452
mapping: {
455453
tracking_type: 'Lead'
456-
},
457-
features: { 'smart-hashing': true }
454+
}
458455
})
459456

460457
expect(responses.length).toBe(1)

packages/destination-actions/src/destinations/reddit-conversions-api/customEvent/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ const action: ActionDefinition<Settings, Payload> = {
2828
event_metadata,
2929
conversion_id
3030
},
31-
perform: async (request, { settings, payload, features }) => {
32-
return await send(request, settings, [payload], features)
31+
perform: async (request, { settings, payload }) => {
32+
return await send(request, settings, [payload])
3333
},
34-
performBatch: async (request, { settings, payload, features }) => {
35-
return await send(request, settings, payload, features)
34+
performBatch: async (request, { settings, payload }) => {
35+
return await send(request, settings, payload)
3636
}
3737
}
3838

packages/destination-actions/src/destinations/reddit-conversions-api/standardEvent/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ const action: ActionDefinition<Settings, Payload> = {
2828
event_metadata,
2929
conversion_id
3030
},
31-
perform: async (request, { settings, payload, features }) => {
32-
return await send(request, settings, [payload], features)
31+
perform: async (request, { settings, payload }) => {
32+
return await send(request, settings, [payload])
3333
},
34-
performBatch: async (request, { settings, payload, features }) => {
35-
return await send(request, settings, payload, features)
34+
performBatch: async (request, { settings, payload }) => {
35+
return await send(request, settings, payload)
3636
}
3737
}
3838

packages/destination-actions/src/destinations/reddit-conversions-api/utils.ts

+18-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Features, RequestClient } from '@segment/actions-core'
1+
import type { RequestClient } from '@segment/actions-core'
22
import type { Settings } from './generated-types'
33
import type { Payload as StandardEvent } from './standardEvent/generated-types'
44
import type { Payload as CustomEvent } from './customEvent/generated-types'
@@ -10,7 +10,7 @@ import {
1010
EventMetadata,
1111
DatapProcessingOptions
1212
} from './types'
13-
import { processHashing } from '../../lib/hashing-utils'
13+
import { processHashingV2 } from '../../lib/hashing-utils'
1414

1515
type EventMetadataType = StandardEvent['event_metadata'] | CustomEvent['event_metadata']
1616
type ProductsType = StandardEvent['products'] | CustomEvent['products']
@@ -19,25 +19,16 @@ type DataProcessingOptionsType = StandardEvent['data_processing_options'] | Cust
1919
type UserType = StandardEvent['user'] | CustomEvent['user']
2020
type ScreenDimensionsType = StandardEvent['screen_dimensions'] | CustomEvent['screen_dimensions']
2121

22-
export async function send(
23-
request: RequestClient,
24-
settings: Settings,
25-
payload: StandardEvent[] | CustomEvent[],
26-
features?: Features
27-
) {
28-
const data = createRedditPayload(payload, settings, features)
22+
export async function send(request: RequestClient, settings: Settings, payload: StandardEvent[] | CustomEvent[]) {
23+
const data = createRedditPayload(payload, settings)
2924
return request(`https://ads-api.reddit.com/api/v2.0/conversions/events/${settings.ad_account_id}`, {
3025
method: 'POST',
3126
headers: { Authorization: `Bearer ${settings.conversion_token}` },
3227
json: JSON.parse(JSON.stringify(data))
3328
})
3429
}
3530

36-
function createRedditPayload(
37-
payloads: StandardEvent[] | CustomEvent[],
38-
settings: Settings,
39-
features?: Features
40-
): StandardEventPayload {
31+
function createRedditPayload(payloads: StandardEvent[] | CustomEvent[], settings: Settings): StandardEventPayload {
4132
const payloadItems: StandardEventPayloadItem[] = []
4233

4334
payloads.forEach((payload) => {
@@ -64,8 +55,8 @@ function createRedditPayload(
6455
custom_event_name: clean(custom_event_name)
6556
},
6657
click_id: clean(click_id),
67-
event_metadata: getMetadata(event_metadata, products, conversion_id, features),
68-
user: getUser(user, data_processing_options, screen_dimensions, features)
58+
event_metadata: getMetadata(event_metadata, products, conversion_id),
59+
user: getUser(user, data_processing_options, screen_dimensions)
6960
}
7061

7162
payloadItems.push(payloadItem)
@@ -105,8 +96,7 @@ function getProducts(products: ProductsType): Product[] | undefined {
10596
function getMetadata(
10697
metadata: EventMetadataType,
10798
products: ProductsType,
108-
conversion_id: ConversionIdType,
109-
features?: Features
99+
conversion_id: ConversionIdType
110100
): EventMetadata | undefined {
111101
if (!metadata && !products && !conversion_id) {
112102
return undefined
@@ -117,18 +107,14 @@ function getMetadata(
117107
item_count: cleanNum(metadata?.item_count),
118108
value_decimal: cleanNum(metadata?.value_decimal),
119109
products: getProducts(products),
120-
conversion_id: smartHash(conversion_id, features, (value) => value.trim())
110+
conversion_id: smartHash(conversion_id, (value) => value.trim())
121111
}
122112
}
123113

124-
function getAdId(
125-
device_type?: string,
126-
advertising_id?: string,
127-
features?: Features
128-
): { [key: string]: string | undefined } | undefined {
114+
function getAdId(device_type?: string, advertising_id?: string): { [key: string]: string | undefined } | undefined {
129115
if (!device_type) return undefined
130116
if (!advertising_id) return undefined
131-
const hashedAdId = smartHash(advertising_id, features)
117+
const hashedAdId = smartHash(advertising_id)
132118
return device_type === 'ios' ? { idfa: hashedAdId } : { aaid: hashedAdId }
133119
}
134120

@@ -154,16 +140,15 @@ function getScreen(height?: number, width?: number): { height: number; width: nu
154140
function getUser(
155141
user: UserType,
156142
dataProcessingOptions: DataProcessingOptionsType,
157-
screenDimensions: ScreenDimensionsType,
158-
features?: Features
143+
screenDimensions: ScreenDimensionsType
159144
): User | undefined {
160145
if (!user) return
161146

162147
return {
163-
...getAdId(user.device_type, user.advertising_id, features),
164-
email: smartHash(user.email, features, canonicalizeEmail),
165-
external_id: smartHash(user.external_id, features, (value) => value.trim()),
166-
ip_address: smartHash(user.ip_address, features, (value) => value.trim()),
148+
...getAdId(user.device_type, user.advertising_id),
149+
email: smartHash(user.email, canonicalizeEmail),
150+
external_id: smartHash(user.external_id, (value) => value.trim()),
151+
ip_address: smartHash(user.ip_address, (value) => value.trim()),
167152
user_agent: clean(user.user_agent),
168153
uuid: clean(user.uuid),
169154
data_processing_options: getDataProcessingOptions(dataProcessingOptions),
@@ -178,11 +163,7 @@ function canonicalizeEmail(value: string): string {
178163
return `${localPart.toLowerCase()}@${localPartAndDomain[1].toLowerCase()}`
179164
}
180165

181-
const smartHash = (
182-
value: string | undefined,
183-
features?: Features,
184-
cleaningFunction?: (value: string) => string
185-
): string | undefined => {
166+
const smartHash = (value: string | undefined, cleaningFunction?: (value: string) => string): string | undefined => {
186167
if (value === undefined) return
187-
return processHashing(value, 'sha256', 'hex', features, 'actions-reddit-conversions-api', cleaningFunction)
168+
return processHashingV2(value, 'sha256', 'hex', cleaningFunction)
188169
}

packages/destination-actions/src/destinations/tiktok-audiences/addToAudience/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
3030
enable_batching: { ...enable_batching },
3131
external_audience_id: { ...external_audience_id }
3232
},
33-
perform: async (request, { audienceSettings, payload, statsContext, features }) => {
33+
perform: async (request, { audienceSettings, payload, statsContext }) => {
3434
const statsClient = statsContext?.statsClient
3535
const statsTag = statsContext?.tags
3636

@@ -42,9 +42,9 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
4242
statsClient?.incr('addToAudience', 1, statsTag)
4343
}
4444

45-
return processPayload(request, audienceSettings, [payload], 'add', features || {})
45+
return processPayload(request, audienceSettings, [payload], 'add')
4646
},
47-
performBatch: async (request, { payload, audienceSettings, statsContext, features }) => {
47+
performBatch: async (request, { payload, audienceSettings, statsContext }) => {
4848
const statsClient = statsContext?.statsClient
4949
const statsTag = statsContext?.tags
5050

@@ -56,7 +56,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
5656
statsClient?.incr('addToAudience', 1, statsTag)
5757
}
5858

59-
return processPayload(request, audienceSettings, payload, 'add', features || {})
59+
return processPayload(request, audienceSettings, payload, 'add')
6060
}
6161
}
6262

packages/destination-actions/src/destinations/tiktok-audiences/addUser/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ const action: ActionDefinition<Settings, Payload> = {
7878
}
7979
}
8080
},
81-
perform: async (request, { settings, payload, statsContext, features }) => {
81+
perform: async (request, { settings, payload, statsContext }) => {
8282
statsContext?.statsClient?.incr('addUserLegacy', 1, statsContext?.tags)
83-
return processPayload(request, settings, [payload], 'add', features || {})
83+
return processPayload(request, settings, [payload], 'add')
8484
},
85-
performBatch: async (request, { settings, payload, statsContext, features }) => {
85+
performBatch: async (request, { settings, payload, statsContext }) => {
8686
statsContext?.statsClient?.incr('addUserLegacy', 1, statsContext?.tags)
87-
return processPayload(request, settings, payload, 'add', features || {})
87+
return processPayload(request, settings, payload, 'add')
8888
}
8989
}
9090

0 commit comments

Comments
 (0)