diff --git a/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts b/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts index bbf61ff870..5bea0775fe 100644 --- a/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts +++ b/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts @@ -13,6 +13,20 @@ interface GetAllAudienceResponse { name: string }[] } + +interface GetSingleAudienceResponse { + name: string + id: string +} + +interface FacebookResponseError { + error: { + message: string + type: string + code: number + } +} + export default class FacebookClient { request: RequestClient baseUrl: string @@ -35,8 +49,16 @@ export default class FacebookClient { }) } - getSingleAudience = async (audienceId: string): void | Error => { - return this.request(`${this.baseUrl}${audienceId}`) + getSingleAudience = async ( + audienceId: string + ): Promise<{ data?: GetSingleAudienceResponse; error?: FacebookResponseError }> => { + try { + const fields = '?fields=id,name' + const { data } = await this.request(`${this.baseUrl}${audienceId}${fields}`) + return { data, error: undefined } + } catch (error) { + return { data: undefined, error: error as FacebookResponseError } + } } getAllAudiences = async (): Promise<{ choices: DynamicFieldItem[]; error: DynamicFieldError | undefined }> => { diff --git a/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/index.ts b/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/index.ts index 9a2af90d4e..90101a3c54 100644 --- a/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/index.ts +++ b/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/index.ts @@ -37,6 +37,12 @@ const action: ActionDefinition = { } }, outputTypes: { + audienceName: { + type: 'string', + label: 'Audience Name', + description: 'The name of the audience in Facebook this mapping is connected to.', + required: true + }, audienceId: { type: 'string', label: 'Audience ID', @@ -48,18 +54,22 @@ const action: ActionDefinition = { const fbClient = new FacebookClient(request, settings.adAccountId) if (hookInputs.existingAudienceId) { - const error = fbClient.getSingleAudience(hookInputs.existingAudienceId) + const { data, error } = await fbClient.getSingleAudience(hookInputs.existingAudienceId) if (error) { return { - error + error: { + message: error.error.message, + code: error.error.type + } } } return { - successMessage: `Audience selected with ID: ${hookInputs.existingAudienceId}`, + successMessage: `Connected to audience with ID: ${hookInputs.existingAudienceId}`, savedData: { - audienceId: hookInputs.existingAudienceId + audienceId: hookInputs.existingAudienceId, + audienceName: data?.name } } } @@ -68,7 +78,8 @@ const action: ActionDefinition = { return { successMessage: `Audience created with ID: ${data.id}`, savedData: { - audienceId: data.id + audienceId: data.id, + audienceName: hookInputs.audienceName } } }