Commit 0bc8bdf4 authored by heshanmw's avatar heshanmw Committed by Maciej Zgadzaj

Issue #2730359 by heshanlk: Provide an API to alter data for the user, node and order actions

parent 748c0fcb
<?php
/**
* @file
* API documentation for Facebook Pixel.
*/
/**
* Alters the final array of data items to be pushed.
*
* Modules can implement hook_facebook_pixel_data_ACTION_alter() to modify
* data sent to Facebook Pixel for a specific action.
*
* Possible actions:
* - Purchase
* - AddToCart
* - InitiateCheckout
* - CompleteRegistration
* - ViewContent
*
* @param array &$$data
* By reference. An array of all encoded data elements.
*
* @param int $entity_id
* Associated entity id.
*/
function hook_facebook_pixel_data_ACTION_alter(&$data, $order_id) {
// Example for 'Purchase' action: Add product ids to the purchase data.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order_id);
$product_ids = array();
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if (in_array($line_item_wrapper->getBundle(), commerce_product_line_item_types())) {
$product_ids[] = $line_item_wrapper->commerce_product->product_id->value();
}
}
$data += array(
'content_ids' => $product_ids,
'content_type' => 'product',
);
}
......@@ -40,7 +40,7 @@ function facebook_pixel_menu() {
* Implements hook_user_insert().
*/
function facebook_pixel_user_insert(&$edit, $account, $category) {
facebook_pixel_add_event('CompleteRegistration');
facebook_pixel_add_event('CompleteRegistration:' . $account->uid);
}
/**
......@@ -74,7 +74,7 @@ function facebook_pixel_ctools_render_alter(&$info, &$page, &$context) {
* (ex: click, landing page on Add to Cart button).
*/
function facebook_pixel_commerce_cart_product_add($order, $product, $quantity, $line_item) {
facebook_pixel_add_event('AddToCart');
facebook_pixel_add_event('AddToCart:' . $order->order_id);
}
/**
......@@ -85,7 +85,7 @@ function facebook_pixel_commerce_cart_product_add($order, $product, $quantity, $
*/
function facebook_pixel_commerce_checkout_router($order, $checkout_page) {
if ($checkout_page['page_id'] == 'checkout') {
facebook_pixel_add_event('InitiateCheckout');
facebook_pixel_add_event('InitiateCheckout:' . $order->order_id);
}
}
......@@ -157,7 +157,9 @@ fbq('track', 'PageView');
if (!empty($entity_id) && function_exists('facebook_pixel_data_' . $event)) {
$data = call_user_func('facebook_pixel_data_' . $event, $entity_id);
$script .= "fbq('track', '$event', $data);\n";
// Allow to alter data.
drupal_alter('facebook_pixel_data_' . $event, $data, $entity_id);
$script .= "fbq('track', '$event', " . drupal_json_encode($data) . ");\n";
}
else {
$script .= "fbq('track', '$event');\n";
......@@ -199,7 +201,7 @@ function facebook_pixel_data_Purchase($order_id) {
'value' => number_format(commerce_currency_amount_to_decimal($order_total['amount'], $order_total['currency_code']), 2, '.', ''),
'currency' => $order_total['currency_code'],
);
return drupal_json_encode($data);
return $data;
}
/**
......@@ -219,7 +221,7 @@ function facebook_pixel_data_ViewContent($nid) {
'content_type' => 'product',
'content_ids' => $nid,
);
return drupal_json_encode($data);
return $data;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment