Skip to content

Instantly share code, notes, and snippets.

@sahava
Last active April 13, 2021 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahava/3eed0a78e90479831b6af0b707b4d5fc to your computer and use it in GitHub Desktop.
Save sahava/3eed0a78e90479831b6af0b707b4d5fc to your computer and use it in GitHub Desktop.
Facebook - Custom Template demo
___INFO___
{
"type": "TAG",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Facebook",
"brand": {
"id": "brand_dummy",
"displayName": "Simmer"
},
"description": "Facebook template for demo purposes.",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "pixelId",
"displayName": "Pixel ID",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "apiAccessToken",
"displayName": "API Access Token",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "testEventCode",
"displayName": "Test Event Code",
"simpleValueType": true
}
]
___SANDBOXED_JS_FOR_SERVER___
// Load the APIs
const getAllEventData = require('getAllEventData');
const getTimestampMillis = require('getTimestampMillis');
const JSON = require('JSON');
const log = require('logToConsole');
const Math = require('Math');
const sendHttpRequest = require('sendHttpRequest');
// Setup constants - modify when the API is updated
const API_ENDPOINT = 'https://graph.facebook.com';
const VERSION = 'v10.0';
// Map GA4 event names to their Facebook counterparts
const eventMap = {
add_payment_info: 'AddPaymentInfo',
add_to_cart: 'AddToCart',
add_to_wishlist: 'AddToWishlist',
begin_checkout: 'InitiateCheckout',
generate_lead: 'Lead',
page_view: 'PageView',
purchase: 'Purchase',
search: 'Search',
signup: 'CompleteRegistration',
view_item: 'ViewContent'
};
const eventModel = getAllEventData();
const event = {};
// Map the GA4 event name if found in the standard event name map,
// use the original event name if no match is found.
const mapEventName = (eventName) => {
return eventMap[eventName] || eventName;
};
// Start building the Facebook data object (event.*) by mapping items in
// the generic event data object (eventModel.*) into the expected format
event.event_name = eventMap[eventModel.event_name] || eventModel.event_name;
event.event_time = eventModel.event_time || (Math.round(getTimestampMillis() / 1000));
event.event_id = eventModel.event_id;
event.event_source_url = eventModel.page_location;
// Keys in the event data model prefixed with "x-fb-" are specific
// to output from a Facebook Client. These need to be replaced with
// values in the generic event data object OR by fields in the tag itself,
// where the template user can hard-code the values if they wish.
event.user_data = {};
event.user_data.client_ip_address = eventModel.ip_override;
event.user_data.client_user_agent = eventModel.user_agent;
event.user_data.fbp = eventModel['x-fb-ck-fbp'];
event.user_data.fbc = eventModel['x-fb-ck-fbc'];
event.custom_data = {};
event.custom_data.currency = eventModel.currency;
event.custom_data.value = eventModel.value;
event.custom_data.search_string = eventModel.search_term;
event.custom_data.content_category = eventModel['x-fb-cd-content_category'];
event.custom_data.content_ids = eventModel['x-fb-cd-content_ids'];
event.custom_data.content_name = eventModel['x-fb-cd-content_name'];
event.custom_data.content_type = eventModel['x-fb-cd-content_type'];
event.custom_data.contents = eventModel['x-fb-cd-contents'];
event.custom_data.num_items = eventModel['x-fb-cd-num_items'];
event.custom_data.predicted_ltv = eventModel['x-fb-cd-predicted_ltv'];
event.custom_data.status = eventModel['x-fb-cd-status'];
event.custom_data.delivery_category = eventModel['x-fb-cd-delivery_category'];
const eventRequest = {data: [event]};
if(data.testEventCode) {
eventRequest.test_event_code = data.testEventCode;
}
// Build the URL for the HTTP request
const routeParams = 'events?access_token=' + data.apiAccessToken;
const graphEndpoint = [API_ENDPOINT,
VERSION,
data.pixelId,
routeParams].join('/');
log('Sending eventRequest', eventRequest);
const requestHeaders = {headers: {'content-type': 'application/json'}, method: 'POST'};
// Send the request in the expected format, log a successful response
sendHttpRequest(
graphEndpoint,
(statusCode, headers, response) => {
log(JSON.stringify(response));
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
return;
}
data.gtmOnFailure();
},
requestHeaders,
JSON.stringify(eventRequest));
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_event_data",
"versionId": "1"
},
"param": [
{
"key": "eventDataAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "send_http",
"versionId": "1"
},
"param": [
{
"key": "allowedUrls",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "urls",
"value": {
"type": 2,
"listItem": [
{
"type": 1,
"string": "https://graph.facebook.com/"
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "logging",
"versionId": "1"
},
"param": [
{
"key": "environments",
"value": {
"type": 1,
"string": "debug"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios: []
setup: ''
___NOTES___
Created on 8/5/2020, 10:20:28 AM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment