Skip to content

Commit

Permalink
remove error tracking logs (#2621)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcgilbert authored Dec 3, 2024
1 parent c4e70e2 commit 23e031d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ const createAudienceInput = {

describe('Snap Audiences', () => {
describe('createAudience', () => {
beforeEach(() => {
// Catch all Segment API requests and return 200
nock('https://api.segment.io').persist().post('/v1/track').reply(200)
})

afterEach(() => {
nock.cleanAll()
})

it('should fail if Segment audience name is available', async () => {
createAudienceInput.audienceName = ''
await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(IntegrationError)
Expand Down
128 changes: 18 additions & 110 deletions packages/destination-actions/src/destinations/snap-audiences/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AudienceDestinationDefinition, defaultValues, IntegrationError, RequestClient } from '@segment/actions-core'
import { AudienceDestinationDefinition, defaultValues, IntegrationError } from '@segment/actions-core'
import type { Settings, AudienceSettings } from './generated-types'
import syncAudience from './syncAudience'
const ACCESS_TOKEN_URL = 'https://accounts.snapchat.com/login/oauth2/access_token'
Expand Down Expand Up @@ -116,101 +116,29 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
const ad_account_id = createAudienceInput.settings.ad_account_id
const { customAudienceName, description, retention_in_days } = createAudienceInput.audienceSettings || {}

// Track input values
await sendToSegment(
'jsYMXBFcHxTHnNfZfwQo5FsL8jVRrjAu',
'Create Audience Input',
ad_account_id,
{
rawInput: createAudienceInput,
extractedValues: {
audienceName,
ad_account_id,
customAudienceName,
description,
retention_in_days
}
},
request
)

if (!audienceName) {
throw new IntegrationError('Missing audience name value', 'MISSING_REQUIRED_FIELD', 400)
}

const requestBody = {
segments: [
{
name: customAudienceName !== '' ? customAudienceName : audienceName,
source_type: 'FIRST_PARTY',
ad_account_id,
description,
retention_in_days
}
]
}

// Track request details
await sendToSegment(
'jsYMXBFcHxTHnNfZfwQo5FsL8jVRrjAu',
'Create Audience Request Details',
ad_account_id,
{
url: `https://adsapi.snapchat.com/v1/adaccounts/${ad_account_id}/segments`,
headers: {
Authorization: 'Bearer [TOKEN HIDDEN]',
'Content-Type': 'application/json'
},
requestBody
},
request
)

try {
const response = await request(`https://adsapi.snapchat.com/v1/adaccounts/${ad_account_id}/segments`, {
method: 'POST',
json: {
segments: [
{
name: customAudienceName !== '' ? customAudienceName : audienceName,
source_type: 'FIRST_PARTY',
ad_account_id,
description,
retention_in_days
}
]
} as CreateAudienceReq
})
const data: SnapAudienceResponse = await response.json()
const snapAudienceId = data.segments[0].segment.id
const response = await request(`https://adsapi.snapchat.com/v1/adaccounts/${ad_account_id}/segments`, {
method: 'POST',
json: {
segments: [
{
name: customAudienceName !== '' ? customAudienceName : audienceName,
source_type: 'FIRST_PARTY',
ad_account_id,
description,
retention_in_days
}
]
} as CreateAudienceReq
})

// Track response
await sendToSegment(
'jsYMXBFcHxTHnNfZfwQo5FsL8jVRrjAu',
'Create Audience Response',
ad_account_id,
{
status: response.status,
responseData: data,
extractedAudienceId: snapAudienceId
},
request
)
const data: SnapAudienceResponse = await response.json()
const snapAudienceId = data.segments[0].segment.id

return { externalId: snapAudienceId }
} catch (error) {
// Error response
await sendToSegment(
'jsYMXBFcHxTHnNfZfwQo5FsL8jVRrjAu',
'Create Audience Error',
ad_account_id,
{
error: error.response.data
},
request
)
}
return { externalId: '1234' }
return { externalId: snapAudienceId }
},

getAudience: async (request, { externalId }) => {
Expand All @@ -229,24 +157,4 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
}
}

async function sendToSegment(writeKey: any, event: string, userId: string, properties: any, request: RequestClient) {
const response = await request('https://api.segment.io/v1/track', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(writeKey).toString('base64')}`
},
json: {
event,
userId,
properties
}
})

if (!response.ok) {
throw new Error(`Failed to send event to Segment: ${response.statusText}`)
}

return response
}
export default destination

0 comments on commit 23e031d

Please sign in to comment.