summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorheshanmw2016-05-20 15:03:19 (GMT)
committerMaciej Zgadzaj2016-05-20 15:03:19 (GMT)
commit0bc8bdf4dfc4154e99f38a84e7395bbb4c894eef (patch)
tree4e6296bb46a52a6d93b514b71aff8834929cb943
parent748c0fcb52fae883b9ca742838d4550de3a8cd03 (diff)
Issue #2730359 by heshanlk: Provide an API to alter data for the user, node and order actions7.x-1.17.x-1.x
-rw-r--r--facebook_pixel.api.php42
-rwxr-xr-xfacebook_pixel.module14
2 files changed, 50 insertions, 6 deletions
diff --git a/facebook_pixel.api.php b/facebook_pixel.api.php
new file mode 100644
index 0000000..51a8110
--- /dev/null
+++ b/facebook_pixel.api.php
@@ -0,0 +1,42 @@
+<?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',
+ );
+}
diff --git a/facebook_pixel.module b/facebook_pixel.module
index 6efbe28..377bca2 100755
--- a/facebook_pixel.module
+++ b/facebook_pixel.module
@@ -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;
}
/**