Question

Currently, I have setup tracking on my wordpress website using piwik. However, on top of that, I need to be able to track the e-commerce conversion and abandoned cart. I have managed to track the conversion manually but I have no idea on how do I replace the hardcoded values with the "woocommerce ordered products" values.

Here are what have I done so far:

  1. Installed WP-Piwik
  2. Installed WooCommerce Piwik integration
  3. I created my custom javascript, called "piwik.js", and I hardcoded the values to track e-commerce purchases.

    function addEcommerceItem() {
    // add the first product to the order
        _paq.push(['addEcommerceItem',
        "sku01", // (required) SKU: Product unique identifier
        "sku01Name", // (optional) Product name
        "productCategory", // (optional) Product category. You can also specify an array of up to 5 categories eg. ["Books", "New releases", "Biography"]
        60, // (recommended) Product price
        1 // (optional, default to 1) Product quantity
        ]);
        alert("e-Commerce item added!");
     };
    
     function trackEcommerceOrder() {       
        _paq.push(['trackEcommerceOrder',
        "A10000127", // (required) Unique Order ID
        35, // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount)
        30, // (optional) Order sub total (excludes shipping)
        5.5, // (optional) Tax amount
        4.5, // (optional) Shipping amount
        false // (optional) Discount offered (set to false for unspecified parameter)
        ]);
      alert("item tracked!");
      };
    
  4. On my Woocommerce, checkout page:

       [woocommerce_checkout]
       <script type="text/javascript" src="http://myServerIP/mySite/piwik.js"></script>
       <script type="text/javascript">
          addEcommerceItem(); 
          trackEcommerceOrder(); 
       </script>
    
  5. And again, my question was how do I replace hardcoded values in addEcommerceItem() and trackEcommerceOrder() to reflect whatever products being purchased through my woocommerce site dynamically?

Thank you in advance

Was it helpful?

Solution

Modified 'piwik-woocommerce-integration' plugin -> 'class-wc-piwik-tracker.php'

  1. Replaced piwikTracker.addEcommerceItem() and its implementation with:

    //add order
    _paq.push(['addEcommerceItem',
       "<?php echo esc_js( $_product->get_sku() ); ?>",
       "<?php echo esc_js( $item['name'] ); ?>",
       "<?php
          if ( isset( $_product->variation_data ) )
             echo esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) );
                ?>",
        <?php echo esc_js( $order->get_item_total( $item ) ); ?>,
        <?php echo esc_js( $item['qty'] ); ?>]);
    
  2. Replaced piwikTracker.trackEcommerceOrder() and its implementation with:

    // Track order
    _paq.push(['trackEcommerceOrder',
       "<?php echo esc_js( $order->get_order_number() ); ?>",
        <?php echo esc_js( $order->get_total() ); ?>,
        false,
        <?php echo esc_js( $order->get_total_tax() ); ?>,
        <?php echo esc_js( $order->get_shipping() ); ?>,
        false
        ]);
        _paq.push(['trackPageView']);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top