Google Analytics eCommerce Variables

Discussion in 'Integration' started by learning_markets, Jul 11, 2014.

  1. learning_markets

    learning_markets New Member

    Joined:
    Nov 29, 2013
    Messages:
    6
    We're using aMember 4.4 and the new version of Google Analytics (Universal Analytics). Setting up the process to track is fairly simple -- we're adding some specific tracking code to the thanks.phtml page as outlined by Google here.

    Where we could use some help is with the appropriate php codes to make the following bit work:

    PHP:
    <?php
    // Transaction Data
    $trans = array('id'=>'1234''affiliation'=>'Acme Clothing',
                  
    'revenue'=>'11.99''shipping'=>'5''tax'=>'1.29');
     
    // List of Items Purchased.
    $items = array(
      array(
    'sku'=>'SDFSDF''name'=>'Shoes''category'=>'Footwear''price'=>'100''quantity'=>'1'),
      array(
    'sku'=>'123DSW''name'=>'Sandles''category'=>'Footwear''price'=>'87''quantity'=>'1'),
      array(
    'sku'=>'UHDF93''name'=>'Socks''category'=>'Footwear''price'=>'5.99''quantity'=>'2')
    );
    ?>
    Anyone know how to call things like 'transaction ID,' 'product,' 'price,' etc from an order thanks page?
  2. learning_markets

    learning_markets New Member

    Joined:
    Nov 29, 2013
    Messages:
    6
    In case anyone is interested here is our latest attempt at installing Google eCommerce tracking code to track sales data in analytics. We dropped this code on the bottom of the thanks.phtml page. This is using Google's recommended approach. I checked it with the GA debugger and it's working -- sort of.

    It seems to be registering the transaction but the RIGHT data is not passing through. I suspect I am not using the right code for things like Order ID or Price. (For example, it sends a price of $0 because we offer free trials. There must be a way to send the recurring price rather than the price the customer pays today.)

    If anyone has any ideas, let me know...

    PHP:
    <!-- Google eCommerce Tracking -->
    <?php
    // Transaction Data
    $trans = array('id'=> $invoice->public_id'revenue'=> $invoice->getCurrency($invoice->first_total));
     
    // Item Purchased.
    $items = array('name'=> $invoice->item_title'price'=> $invoice->getCurrency($invoice->first_subtotal));
    ?>
     
    <?php
    // Function to return the JavaScript representation of a TransactionData object.
    function getTransactionJs(&$trans) {
      return <<<HTML
    ga('ecommerce:addTransaction', {
      'id': '
    {$trans['id']}',
      'revenue': '
    {$trans['revenue']}'
    });
    HTML;
    }
     
    // Function to return the JavaScript representation of an ItemData object.
    function getItemJs(&$transId, &$item) {
      return <<<HTML
    ga('ecommerce:addItem', {
      'id': '
    $transId',
      'name': '
    {$item['name']}',
      'price': '
    {$item['price']}'
    });
    HTML;
    }
    ?>
     
    <script>
    ga('require', 'ecommerce');
     
    <?php
    echo getTransactionJs($trans);
    echo 
    getItemJs($trans['id'], $item);
    ?>
     
    ga('ecommerce:send');   
    </script>
  3. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Hmm why do you need to send recurring price, if user didn't pay it yet?
    You can get recurring price from this variable: $invoice->second_total
  4. tomingles

    tomingles Member

    Joined:
    Mar 27, 2013
    Messages:
    52
    Hi learning_markets and Alexander. I'm about to migrate our Amember Pro 4.5.3 to using Universal Analytics, but also want to manage this through Google Tag Manager. As this is related to what you were discussing above, I thought I would add it to this thread instead of starting a new one. I hope that's OK.

    In Tag Manager (V2), I think the correct way to do this is to load all the transaction data into the "data layer" then fire this off to Analytics using a trigger defined in Google Tag Manager, which can be triggered simply with a page view of the thank you page. See documentation here.

    The example data layer code that Google provides looks like this:

    Code:
    <script>
    dataLayer = [{
        'transactionId': '1234',
        'transactionAffiliation': 'Acme Clothing',
        'transactionTotal': 38.26,
        'transactionTax': 1.29,
        'transactionShipping': 5,
        'transactionProducts': [{
            'sku': 'DD44',
            'name': 'T-Shirt',
            'category': 'Apparel',
            'price': 11.99,
            'quantity': 1
        },{
            'sku': 'AA1243544',
            'name': 'Socks',
            'category': 'Apparel',
            'price': 9.99,
            'quantity': 2
        }]
    }];
    </script>
    My questions are:

    1) learning_markets: Did you manage to get your previous implementation working correctly using the code above?

    2) I'm assuming you also edited \application\default\plugins\misc\google-analytics.php in some way to change this to include the default Universal Analytics snippet? Or did you simply include this yourself in some customised template files and bypass the Amember Analytics plugin?

    If one of the developers could perhaps provide some guidance on implementing Universal Analytics with/without Google Tag Manager, it would be really appreciated!

    Thanks!

Share This Page