Create optimized Facebook Ads targeted at the right audience. With the Profiles API Token and Segments, you can make a list of visitors who already access your website or are registered on your website and then send optimized Facebook Ads to them.
What’s the difference between this integration and Writing Job Results into Facebook Custom Audience?
Using Facebook Custom Audience requires user identifier information, for example, email or phone number. The Profiles API integration enables you to send a segment to Facebook Custom Audience without such parameters. Instead, you use Facebook Pixel.
- Prerequisites
- Create the Profiles API Token
- Name Your New Token
- Configure the Token
- Add Segments to the Token
- Integrating TD Profiles API with Facebook using Pixel IDs
- Get Facebook Pixel ID
- Set up Javascript on your Website
- Facebook Javascript Tag
- Treasure Data Javascript SDK
- Case v1.9.x
- Case v2.x.x
- Confirm Integration Status
- Create Custom Audience
Basic knowledge of Treasure Data
Facebook Business Account
- Open TD Console.
- Navigate to Audience Studio and select a segment.
- Select a folder.
- Select Create New.
- Select Profile API Token.

- Enter a name for your token and optionally enter a description. Select Next.
The name and description carry over from when you named the token. You can make changes.
- Enter a Lookup Key.
A Lookup Key is a column with a unique value inside a parent segment. This key identifies the correct profile.
The lookup key update will take effect once the token workflow finishes.
When using td_global_id as the lookup key, the Profiles API retrieves the lookup value from the browser cookie instead of the API request's query param. For testing or verification purposes outside of a browser (eg, Postman), use a different column to pass in the lookup value manually.
- (Optional) Enter a maximum of five attributes.
Profile attributes can be returned from the profile token when queried.

- Choose one of the following:
- Select Next to continue and add Segments.
- Select Create to configure the token. (You can add segments later if you want.)

- Choose one of the following:
- After you configure your token, select Next.
- Navigate to Audience Studio > Folder > Profiles API Token.

- Add segments to your Profiles API Token.
- Select Save.

Your Facebook Pixel ID is retrieved on your Facebook Business Account. Refer to Facebook's documentation on finding your Pixel ID.

Integrate Facebook Javascript Tag with your website. In the script, replace “YOUR_PIXEL_ID” with your pixel ID.
See more details at: https://developers.facebook.com/docs/facebook-pixel/implementation
We provide two versions of Javascript SDK. The version you use affects the response format of the fetchUserSegment function, which is used for this procedure. We recommend that you use Javascript SDK version 3.
The fetchUserSegments function returns associated segment ids when a visitor accesses your website. Adding the segment ids into the fbq function with trackCustom parameter will send them to Facebook.
The parameters in the fbq function require; (1): Event Name (any), (2): Column name on Facebook (any), (3): Segment ID from the fetchUserSegment function.
<script type="text/javascript">
var td = new Treasure({
...
});
var successCallback = function (key, segments) {
// send to FB
fbq('trackCustom','event_tracking',{'td_segment_id': segments.join(',')}); // fbq('trackCustom','(1)',{'(2)': (3)});
};
td.fetchUserSegments({
audienceToken: [token1, token2],
keys: {some_key: 'value'}
}, successCallback);
</script>Because JS SDK v2 or later returns a different response format from v1.9 or earlier, you must extract only segment ids from the fetchUserSegments function in an additional logic step. Then, you add the segment ids into the fbq function using the trackCustom parameter to send the ids to Facebook.
The parameters in fbq function requires; (1): Event Name (any), (2): Column name on Facebook (any), (3): Segment ID from the fetchUserSegment function.
<script type="text/javascript">
var td = new Treasure({
...
});
function sendSegmentId2Fb(seg) {
var segIdAll = [];
for (var i = 0; i < seg.length; i++) {
segIdAll = segIdAll.concat(seg[i].values);
};
var segId = segIdAll.filter(function (x,i,self) {return self.indexOf(x) === i;});
// send to FB
fbq('trackCustom','event_tracking',{'td_segment_id': segId.join(',')}); // fbq('trackCustom','(1)',{'(2)': (3)});
};
var successCallback = function(values) {
sendSegmentId2Fb(values);
};
td.fetchUserSegments({
audienceToken: [token1, token2],
keys: {some_key: 'value'}
}, successCallback);
</script>After the Javascript integration is triggered, you can see the status of data integration on the Facebook Business Account page.
- Go to the Pixels in the Events Manager section.

Choose a pixel that you integrated.
Select the Activity menu and then review the tracking target name you called using the fbq function.

After the data is stored on the Facebook side, you can create a custom audience by using the data.
- Go to Event Managers and select Pixels.

- Select your pixel.

- Select Create Custom Audience.

- Choose the conditions for which you want to send ads:
- Select an event name
- Select a condition
- Set your custom audience name.
- Review the status of your custom audience.

You can see the status of your custom audience.
