Skip to content

Commit

Permalink
Change name to Conversion Pixel, add Purchase events
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti Kuosmanen committed Dec 1, 2015
1 parent 95e8218 commit 5157e90
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 40 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "anttiviljami/wc-fb-conversion-tracking",
"name": "anttiviljami/woocommerce-facebook-conversion-pixel",
"description": "Set up the Facebook conversion pixel and event tracking for WooCommerce",
"type": "wordpress-plugin",
"license": "GPLv3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

class WC_Integration_Facebook_Conversion_Tracking extends WC_Integration {
class WC_Integration_Facebook_Conversion_Pixel extends WC_Integration {
/**
* Constructor, sets up all the actions
*/
public function __construct() {
$this->id = 'wc-fb-conversion-tracking';
$this->method_title = __( 'Facebook', 'wc-fb-conversion-tracking' );
$this->method_description = __( 'Set up the Facebook conversion pixel and event tracking for WooCommerce.', 'wc-fb-conversion-tracking' );
$this->id = 'wc-fb-conversion-pixel';
$this->method_title = __( 'Facebook', 'wc-fb-conversion-pixel' );
$this->method_description = __( 'Set up the Facebook conversion pixel and event tracking for WooCommerce.', 'wc-fb-conversion-pixel' );

// Load the settings.
$this->init_form_fields();
Expand Down Expand Up @@ -36,35 +36,35 @@ public function __construct() {
* User configurable settings
*/
public function init_form_fields() {
$this->form_fields = apply_filters( 'wc_integration_fb_tracking_fields', array(
$this->form_fields = apply_filters( 'wc_integration_fb_conversion_pixel_fields', array(
'fbid' => array(
'title' => __( 'Facebook Tracking ID', 'wc-fb-conversion-tracking' ),
'title' => __( 'Facebook Tracking ID', 'wc-fb-conversion-pixel' ),
'type' => 'text',
'description' => __( "The numerical unique ID from your Facebook Pixel Tracking Code. Copied from this line: <code>fbq('init', '<your ID>');</code>", 'wc-fb-conversion-tracking' ),
'description' => __( "The numerical unique ID from your Facebook Pixel Tracking Code. Copied from this line: <code>fbq('init', '<your ID>');</code>", 'wc-fb-conversion-pixel' ),
'placeholder' => '123456789',
),
'fb_event_viewcontent' => array(
'title' => __( 'Track ViewContent Events', 'wc-fb-conversion-tracking' ),
'title' => __( 'Track ViewContent Events', 'wc-fb-conversion-pixel' ),
'type' => 'checkbox',
'label' => __( 'Enable event tracking for when a user views a product page.', 'wc-fb-conversion-tracking' ),
'label' => __( 'Enable event tracking for when a user views a product page.', 'wc-fb-conversion-pixel' ),
'default' => 'yes',
),
'fb_event_addtocart' => array(
'title' => __( 'Track AddToCart Events', 'wc-fb-conversion-tracking' ),
'title' => __( 'Track AddToCart Events', 'wc-fb-conversion-pixel' ),
'type' => 'checkbox',
'label' => __( 'Enable event tracking for when a user adds a product to their shopping cart.', 'wc-fb-conversion-tracking' ),
'label' => __( 'Enable event tracking for when a user adds a product to their shopping cart.', 'wc-fb-conversion-pixel' ),
'default' => 'yes',
),
'fb_event_checkout' => array(
'title' => __( 'Track InitiateCheckout Events', 'wc-fb-conversion-tracking' ),
'title' => __( 'Track InitiateCheckout Events', 'wc-fb-conversion-pixel' ),
'type' => 'checkbox',
'label' => __( 'Enable event tracking for when a user enters the Checkout page.', 'wc-fb-conversion-tracking' ),
'label' => __( 'Enable event tracking for when a user enters the Checkout page.', 'wc-fb-conversion-pixel' ),
'default' => 'yes',
),
'fb_event_purchase' => array(
'title' => __( 'Track Purchase Events', 'wc-fb-conversion-tracking' ),
'title' => __( 'Track Purchase Events', 'wc-fb-conversion-pixel' ),
'type' => 'checkbox',
'label' => __( 'Enable event tracking for when a user has succesfully made an order.', 'wc-fb-conversion-tracking' ),
'label' => __( 'Enable event tracking for when a user has succesfully made an order.', 'wc-fb-conversion-pixel' ),
'default' => 'yes',
),
));
Expand Down Expand Up @@ -121,21 +121,46 @@ public function fb_tracking_pixel() {
global $product;
$params = array();
$params['content_name'] = $product->get_title();
$params['content_ids'] = array( $product->id );
$params['content_ids'] = array( $product->get_sku() ? $product->get_sku() : $product->id );
$params['content_type'] = 'product';
$params['value'] = floatval( $product->get_price() );
$params['currency'] = get_woocommerce_currency();
?>
fbq('track', 'ViewContent', <?php echo json_encode( $params ); ?>);
<?php endif; ?>

<?php if( is_checkout() && $this->event_checkout ) : ?>
<?php if( is_order_received_page() && $this->event_purchase ) : ?>
<?php
global $wp;
$params = array();

$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
if( $order_id ) {
$order = new WC_Order( $order_id );
if( $order->get_items() ) {
$productids = array();
foreach ( $order->get_items() as $item ) {
$product = $order->get_product_from_item( $item );
$productids[] = $product->get_sku() ? $product->get_sku() : $product->id;
}
$params['content_ids'] = $productids;
}
$params['content_type'] = 'product';
$params['value'] = $order->get_total();
$params['currency'] = get_woocommerce_currency();
}
?>
fbq('track', 'Purchase', <?php echo json_encode( $params ); ?>);

<?php elseif( is_checkout() && $this->event_checkout ) : ?>
<?php
// get $cart to params
$cart = WC()->cart->get_cart();
$productids = array();
foreach($cart as $id => $product) {
$productids[] = $product['product_id'];
foreach($cart as $id => $item) {
$product_id = $item['variation_id'] ? $item['variation_id'] : $item['product_id'];
$product = new WC_Product( $product_id );
$productids[] = $product->get_sku() ? $product->get_sku() : $product->id;
}
$params = array();
$params['num_items'] = WC()->cart->cart_contents_count;
Expand All @@ -146,14 +171,6 @@ public function fb_tracking_pixel() {
fbq('track', 'InitiateCheckout', <?php echo json_encode( $params ); ?>);
<?php endif; ?>

<?php if( is_order_received_page() && $this->event_purchase ) : ?>
<?php
global $product;
$params = array();
//TODO
?>
fbq('track', 'Purchase', <?php echo json_encode( $params ); ?>);
<?php endif; ?>

</script>
<noscript><img height="1" width="1" style="display:none"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WooCommerce Facebook Conversion Tracking
# WooCommerce Facebook Conversion Pixel

Set up the Facebook conversion pixel and event tracking for WooCommerce

Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=== WooCommerce Facebook Conversion Tracking ===
=== WooCommerce Facebook Conversion Pixel ===
Contributors: Zuige
Tags: Woocommerce, Facebook, Conversion, Tracking, Pixel, Events
Donate link: https://seravo.fi/
Expand Down
20 changes: 10 additions & 10 deletions wc-fb-conversion-tracking.php → wc-fb-conversion-pixel.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* Plugin name: WooCommerce Facebook Conversion Tracking
* Plugin URI: https://github.com/anttiviljami/wc-fb-conversion-tracking
* Plugin name: WooCommerce Facebook Conversion Pixel
* Plugin URI: https://github.com/anttiviljami/woocommerce-facebook-conversion-pixel
* Description: Set up the Facebook conversion pixel and event tracking for WooCommerce
* Version: 0.1
* Author: Seravo Oy
* Author: http://seravo.fi
* License: GPLv3
* Text Domain: wc-fb-conversion-tracking
* Text Domain: wc-fb-conversion-pixel
*/

/** Copyright 2015 Seravo Oy
Expand All @@ -23,14 +23,14 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if ( !class_exists('WooCommerce_Facebook_Conversion_Tracking')) :
if ( !class_exists('WooCommerce_Facebook_Conversion_Pixel')) :

class WooCommerce_Facebook_Conversion_Tracking {
class WooCommerce_Facebook_Conversion_Pixel {
public static $instance;

public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new WooCommerce_Facebook_Conversion_Tracking();
self::$instance = new WooCommerce_Facebook_Conversion_Pixel();
}
return self::$instance;
}
Expand All @@ -44,7 +44,7 @@ private function __construct() {
* Load our plugin textdomain
*/
public static function load_our_textdomain() {
load_plugin_textdomain( 'wc-fb-conversion-tracking', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
load_plugin_textdomain( 'wc-fb-conversion-pixel', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
}

/**
Expand All @@ -53,7 +53,7 @@ public static function load_our_textdomain() {
public static function load_integrations() {
if ( class_exists( 'WC_Integration' ) ) {
// load our integration class
include_once 'inc/class-wc-integration-fb-conversion-tracking.php';
include_once 'inc/class-wc-integration-fb-conversion-pixel.php';

// add to the WooCommerce settings page
add_filter( 'woocommerce_integrations', __CLASS__ . '::add_integration' );
Expand All @@ -64,12 +64,12 @@ public static function load_integrations() {
* Add integration settings pages
*/
public static function add_integration($integrations) {
$integrations[] = 'WC_Integration_Facebook_Conversion_Tracking';
$integrations[] = 'WC_Integration_Facebook_Conversion_Pixel';
return $integrations;
}
}

endif;

// init the plugin
$woocommerce_facebook_conversion_tracking = WooCommerce_Facebook_Conversion_Tracking::init();
$woocommerce_facebook_conversion_tracking = WooCommerce_Facebook_Conversion_Pixel::init();

0 comments on commit 5157e90

Please sign in to comment.