Skip to content

Commit

Permalink
WIP - first draft of a FacebookClient class, including an untested cr…
Browse files Browse the repository at this point in the history
…eateAudience hook for retl
  • Loading branch information
nick-Ag committed Jul 16, 2024
1 parent 5ba6c09 commit 60d2291
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { RequestClient } from '@segment/actions-core'

const FACEBOOK_API_VERSION = 'v20.0'

interface AudienceCreationResponse {
id: string
message: string
}

export default class FacebookClient {
request: RequestClient
baseUrl: string
adAccountId: string

constructor(request: RequestClient, adAccountId: string) {
this.request = request
this.baseUrl = `https://graph.facebook.com/${FACEBOOK_API_VERSION}/`
this.adAccountId = adAccountId
}

createAudience = async (name: string) => {
return this.request<AudienceCreationResponse>(`${this.baseUrl}${this.adAccountId}/customaudiences`, {
method: 'post',
json: {
name,
subtype: 'CUSTOM',
description: 'TODO: Hardcoded for now'
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
import type { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import FacebookClient from '../fbca-operations'

const action: ActionDefinition<Settings, Payload> = {
title: 'Sync Audience',
description: 'Sync data to Facebook Custom Audiences.',
fields: {},
hooks: {
retlOnMappingSave: {
label: 'Select or create an audience in Facebook',
description: 'TODO: Create or select an audience in Facebook.',
inputFields: {
audienceName: {
type: 'string',
label: 'Audience Name',
description: 'The name of the audience in Facebook.',
default: 'TODO: Model Name by default'
}
},
outputTypes: {
audienceId: {
type: 'string',
label: 'Audience ID',
description: 'The ID of the audience in Facebook.',
required: true
}
},
performHook: async (request, { settings, hookInputs }) => {
const fbClient = new FacebookClient(request, settings.adAccountId)

const { data } = await fbClient.createAudience(hookInputs.audienceName)

return {
successMessage: `Audience created with ID: ${data.id}`,
savedData: {
audienceId: data.id
}
}
}
}
},
perform: (_request, _data) => {
// Make your partner api request here!
// return request('https://example.com', {
Expand Down

0 comments on commit 60d2291

Please sign in to comment.