Last active
April 13, 2021 06:42
-
-
Save sahava/25e8bb254ba5efc2b3cd3d9edcdeb633 to your computer and use it in GitHub Desktop.
Facebook (updated) - Custom Template demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
___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 | |
}, | |
{ | |
"type": "SELECT", | |
"name": "fbp_cookie", | |
"displayName": "_fbp Cookie", | |
"macrosInSelect": true, | |
"selectItems": [], | |
"simpleValueType": true, | |
"notSetText": "Choose the _fbp Cookie Source" | |
}, | |
{ | |
"type": "SELECT", | |
"name": "fbc_cookie", | |
"displayName": "_fbc Cookie", | |
"macrosInSelect": true, | |
"selectItems": [], | |
"simpleValueType": true, | |
"notSetText": "Choose the _fbc Cookie Source" | |
}, | |
{ | |
"type": "SELECT", | |
"name": "content_type", | |
"displayName": "Content Type (optional)", | |
"macrosInSelect": false, | |
"selectItems": [ | |
{ | |
"value": "product", | |
"displayValue": "product" | |
}, | |
{ | |
"value": "product_group", | |
"displayValue": "product_group" | |
} | |
], | |
"simpleValueType": true, | |
"notSetText": "Choose a Content Type" | |
} | |
] | |
___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 = {}; | |
const mapEventName = (eventName) => { | |
return eventMap[eventName] || eventName; | |
}; | |
// Helper function to turn the GA4 items array into an array of just the product IDs | |
const generateContentIds = (items) => { | |
return items.map(item => item.item_id); | |
}; | |
// 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; | |
event.user_data = {}; | |
event.user_data.client_ip_address = eventModel.ip_override; | |
event.user_data.client_user_agent = eventModel.user_agent; | |
// Fetch _fbp and _fbc cookie values from corresponding tag fields | |
event.user_data.fbp = data.fbp_cookie; | |
event.user_data.fbc = data.fbc_cookie; | |
// Remove all the special fields (can be added in if necessary), | |
// and generate content_ids automatically from the items array. | |
// Set content_type to the corresponding tag field value. | |
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_ids = generateContentIds(eventModel.items || []); | |
event.custom_data.content_type = data.content_type; | |
const eventRequest = {data: [event]}; | |
if(data.testEventCode) { | |
eventRequest.test_event_code = data.testEventCode; | |
} | |
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'}; | |
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