Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(facebook_conversions_api): updated fb_pixel to support facebook_conversions_api #2641

Closed
wants to merge 8 commits into from
1 change: 1 addition & 0 deletions src/constants/destinationCanonicalNames.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const DestHandlerMap = {
ga360: 'ga',
facebook_conversions_api: 'facebook_pixel',
};

const DestCanonicalNames = {
Expand Down
1 change: 1 addition & 0 deletions src/v0/destinations/facebook_pixel/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CONFIG_CATEGORIES = {
type: 'identify',
name: 'FBPIXELUserDataConfig',
},
APPDATA: { name: 'FBAppEventsConfig' },
COMMON: { name: 'FBPIXELCommonConfig' },
SIMPLE_TRACK: {
standard: false,
Expand Down
100 changes: 100 additions & 0 deletions src/v0/destinations/facebook_pixel/data/FBAppEventsConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[
{
"destKey": "advertiser_tracking_enabled",
"sourceKeys": "context.device.adTrackingEnabled",
"required": true
},
{
"destKey": "application_tracking_enabled",
"sourceKeys": "properties.application_tracking_enabled",
"required": true
},
{
"destKey": "extinfo.0",
"sourceKeys": "context.device.type",
"metadata": {
"type": "toLower"
}
},
{
"destKey": "extinfo.1",
"sourceKeys": "context.app.namespace"
},
{
"destKey": "extinfo.2",
"sourceKeys": "context.app.build"
},
{
"destKey": "extinfo.3",
"sourceKeys": "context.app.version"
},
{
"destKey": "extinfo.4",
"sourceKeys": "context.os.version",
"required": true
},
{
"destKey": "extinfo.5",
"sourceKeys": "context.device.model"
},
{
"destKey": "extinfo.6",
"sourceKeys": "context.locale"
},
{
"destKey": "extinfo.7",
"sourceKeys": "context.abv_timezone"
},
{
"destKey": "extinfo.8",
"sourceKeys": "context.network.carrier"
},
{
"destKey": "extinfo.9",
"sourceKeys": "context.screen.width"
},
{
"destKey": "extinfo.10",
"sourceKeys": "context.screen.height"
},
{
"destKey": "extinfo.11",
"sourceKeys": "context.screen.density"
},
{
"destKey": "extinfo.12",
"sourceKeys": "context.cpu_cores"
},
{
"destKey": "extinfo.13",
"sourceKeys": "context.ext_storage_size"
},
{
"destKey": "extinfo.14",
"sourceKeys": "context.avl_storage_size"
},
{
"destKey": "extinfo.15",
"sourceKeys": "context.timezone"
},
{
"destKey": "campaign_ids",
"sourceKeys": ["properties.campaignId", "context.traits.campaignId", "context.campaign.name"]
},
{
"destKey": "install_referrer",
"sourceKeys": "properties.install_referrer"
},
{
"destKey": "installer_package",
"sourceKeys": "properties.installer_package"
},
{
"destKey": "url_schemes",
"sourceKeys": "properties.url_schemes"
},
{
"destKey": "windows_attribution_id",
"sourceKeys": "properties.windows_attribution_id"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,15 @@
"destKey": "fb_login_id",
"sourceKeys": "context.fb_login_id",
"required": false
},
{
"destKey": "madid",
"sourceKeys": "context.device.advertisingId",
"required": false
},
{
"destKey": "anon_id",
"sourceKeys": "context.device.advertisingId",
"required": false
}
]
21 changes: 17 additions & 4 deletions src/v0/destinations/facebook_pixel/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
transformedPayloadData,
getActionSource,
fetchUserData,
fetchAppData,
handleProduct,
handleSearch,
handleProductListViewed,
Expand All @@ -34,10 +35,11 @@

const responseBuilderSimple = (message, category, destination, categoryToContent) => {
const { Config } = destination;
const { pixelId, accessToken } = Config;
const { pixelId, datasetId, accessToken } = Config;
const identifier = pixelId || datasetId;

if (!pixelId) {
throw new ConfigurationError('Pixel Id not found. Aborting');
if (!identifier) {
throw new ConfigurationError('Pixel Id/Dataset Id not found. Aborting');
}

if (!accessToken) {
Expand All @@ -52,10 +54,12 @@
testDestination,
testEventCode,
standardPageCall,
overrideActionSource,
actionSource,
} = Config;
const integrationsObj = getIntegrationsObj(message, 'fb_pixel');

const endpoint = `https://graph.facebook.com/v17.0/${pixelId}/events?access_token=${accessToken}`;
const endpoint = `https://graph.facebook.com/v17.0/${identifier}/events?access_token=${accessToken}`;

const userData = fetchUserData(message, Config);

Expand All @@ -64,6 +68,9 @@

commonData = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.COMMON.name], 'fb_pixel');
commonData.action_source = getActionSource(commonData, message?.channel);
if (overrideActionSource) {
commonData.action_source = actionSource;

Check warning on line 72 in src/v0/destinations/facebook_pixel/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/transform.js#L72

Added line #L72 was not covered by tests
}

if (category.type !== 'identify') {
customData = flattenJson(
Expand Down Expand Up @@ -177,12 +184,18 @@
}
}

let appData = {};
if (commonData.action_source === 'app' && datasetId) {
appData = fetchAppData(message);

Check warning on line 189 in src/v0/destinations/facebook_pixel/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/transform.js#L189

Added line #L189 was not covered by tests
}

// content_category should only be a string ref: https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/custom-data

return formingFinalResponse(
userData,
commonData,
customData,
appData,
endpoint,
testDestination,
testEventCode,
Expand Down
36 changes: 34 additions & 2 deletions src/v0/destinations/facebook_pixel/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
constructPayload,
defaultPostRequestConfig,
defaultRequestConfig,
isAppleFamily,
} = require('../../util');
const { ACTION_SOURCES_VALUES, CONFIG_CATEGORIES, MAPPING_CONFIG } = require('./config');

Expand Down Expand Up @@ -302,6 +303,31 @@
return userData;
};

const fetchAppData = (message) => {
const appData = constructPayload(

Check warning on line 307 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L307

Added line #L307 was not covered by tests
message,
MAPPING_CONFIG[CONFIG_CATEGORIES.APPDATA.name],
'fb_pixel',
);

if (appData) {
let sourceSDK = appData.extinfo[0];

Check warning on line 314 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L314

Added line #L314 was not covered by tests
if (sourceSDK === 'android') {
sourceSDK = 'a2';

Check warning on line 316 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L316

Added line #L316 was not covered by tests
} else if (isAppleFamily(sourceSDK)) {
sourceSDK = 'i2';
} else {

Check warning on line 319 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L318-L319

Added lines #L318 - L319 were not covered by tests
// if the sourceSDK is not android or ios
throw new InstrumentationError(

Check warning on line 321 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L321

Added line #L321 was not covered by tests
'Extended device information i.e, "context.device.type" is required',
);
}
appData.extinfo[0] = sourceSDK;

Check warning on line 325 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L325

Added line #L325 was not covered by tests
}

return appData;

Check warning on line 328 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L328

Added line #L328 was not covered by tests
};

/**
*
* @param {*} message Rudder element
Expand Down Expand Up @@ -480,6 +506,7 @@
userData,
commonData,
customData,
appData,
endpoint,
testDestination,
testEventCode,
Expand All @@ -488,11 +515,15 @@
const response = defaultRequestConfig();
response.endpoint = endpoint;
response.method = defaultPostRequestConfig.requestMethod;
const jsonStringify = JSON.stringify({
const jsonData = {
user_data: userData,
...commonData,
custom_data: customData,
});
};
if (Object.keys(appData).length > 0) {
jsonData.appData = appData;

Check warning on line 524 in src/v0/destinations/facebook_pixel/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/facebook_pixel/utils.js#L524

Added line #L524 was not covered by tests
}
const jsonStringify = JSON.stringify(jsonData);
const payload = {
data: [jsonStringify],
};
Expand All @@ -516,6 +547,7 @@
transformedPayloadData,
getActionSource,
fetchUserData,
fetchAppData,
handleProduct,
handleSearch,
handleProductListViewed,
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/data/facebook_pixel_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@
}
},
{
"error": "Pixel Id not found. Aborting",
"error": "Pixel Id/Dataset Id not found. Aborting",
"statusCode": 400,
"statTags": {
"errorCategory": "dataValidation",
Expand Down
Loading