1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Please Shoot me now: Google Analytics E-Commerce Tracking JavaScript Hell

Discussion in 'HTML & JavaScript' started by likwidneo, Oct 22, 2012.

  1. likwidneo

    likwidneo Newbie

    Joined:
    Jan 29, 2011
    Messages:
    34
    Likes Received:
    1
    So I've been trying to fix this issue ever since I landed this job a couple weeks back. They have had Google Analytics accounts but they never installed e-commerce conversion tracking. If you aren't familiar with GA E-Commerce tracking, this is NOT the regular traffic tracking code, nor is it simply a question of setting up a Conversion Goal in the account. This is a completely different JavaScript code, and a much more complicated one, especially if you don't know JavaScript like me. It's not just a simple copy and paste because this script relies on pulling the values of a series of variables that are defined by your e-commerce shopping cart platform, and every platform defines these variables differently. I've got the code to the point where it IS reporting data to the account successfully, but the data it is reporting is completely incorrect and in some cases is reporting the variable name itself rather than the value of the variable. Unfortunately I will grow a third nut before I convince my employers to use a different CMS, so I have to work with what I got. This is what my CMS support knowledge base has to say on the issue and it details the variables here:
    A sample script can be found by Googling the phrase
    selecting the first search result listed, and scrolling to the bottom of that page and clicking on the Traditional Syntax bullet. Sorry, I don't have permission to post links yet.This is a copy of the code as I placed it in my order confirmation page, with my GA UA# edited out and anything that BHW might pick up as a URL slightly edited:

    Code:
    <script type="text/javascript">
      var gaJsHost = (("htpps:" == document.location.protocol ) ? "htpps://ssl." : "htpp://ww.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analyticscom/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try{
      var pageTracker = _gat._getTracker("UA-BLACKHATWORLD-1");
      pageTracker._trackPageview();
      pageTracker._addTrans(
          "Order[0]",            // order ID - required
          "Allforfloors.com",  // affiliation or store name
          "Order[2]",           // total - required
          "Order[4]",            // tax
          "Order[5]",           // shipping
          "Order[6]",        // city
          "Order[7]",      // state or province
          "Order[8]"              // country
        );
    
    
    
    
       // add item might be called for every item in the shopping cart
       // where your ecommerce engine loops through each item in the cart and
       // prints out _addItem for each 
       pageTracker._addItem(
          "OrderDetails[X][0]",           // order ID - necessary to associate item with transaction
          "OrderDetails[X][2]",           // SKU/code - required
          "OrderDetails[X][3]",        // product name
          "OrderDetails[X][4]",   // category or variation
          "OrderDetails[X][5]",          // unit price - required
          "OrderDetails[X][6]"               // quantity - required
       );
    
    
       pageTracker._trackTrans(); //submits transaction to the Analytics servers
    } catch(err) {}
    </script>

    I'm pretty sure at this point it has to be something wrong with the script itself, but it is beyond my knowledge of JS. My placement of it within the CMS isn't the problem, I've triple checked it and I have other scripts in the same field that are working perfectly. Any help any JS Pro's can provide would be greatly appreciated.
     
    Last edited: Oct 22, 2012
  2. sockpuppet

    sockpuppet Junior Member

    Joined:
    Nov 7, 2011
    Messages:
    155
    Likes Received:
    145
    try this
    Code:
    
      pageTracker._addTrans(
          Order[0],            // order ID - required
          "Allforfloors.com",  // affiliation or store name
          Order[2],           // total - required
          Order[4],            // tax
          Order[5],           // shipping
          Order[6],        // city
          Order[7],      // state or province
          Order[8]              // country
        );
    
        for(var i=0;i<OrderDetails.length;++i){
            pageTracker._addItem(
                OrderDetails[i][0],           // order ID - necessary to associate item with transaction
                OrderDetails[i][2],           // SKU/code - required
                OrderDetails[i][3],        // product name
                OrderDetails[i][4],   // category or variation
                OrderDetails[i][5],          // unit price - required
                OrderDetails[i][6]               // quantity - required
         );
       }
    
    
     
    • Thanks Thanks x 1
  3. likwidneo

    likwidneo Newbie

    Joined:
    Jan 29, 2011
    Messages:
    34
    Likes Received:
    1
    thanks for the help. the one thing I noticed though is you placed a bracket opening right after your "For" statement but didn't close off the earlier bracket opening placed after "try" Is it supposed to be this way or should I close off the earlier bracket after the first ");" ?
     
  4. sockpuppet

    sockpuppet Junior Member

    Joined:
    Nov 7, 2011
    Messages:
    155
    Likes Received:
    145
    It is supposed to be this way.
    You just need to place this code between
    Code:
    pageTracker._trackPageview();
    and
    Code:
    pageTracker._trackTrans();
     
    • Thanks Thanks x 2
  5. InternetMayhem

    InternetMayhem Regular Member

    Joined:
    Dec 30, 2011
    Messages:
    236
    Likes Received:
    22
    Location:
    California
    Facing the same thing, seems to be a popular request.
     
  6. blackkitty

    blackkitty Newbie

    Joined:
    Jan 27, 2013
    Messages:
    2
    Likes Received:
    0
    I have a similar problem in that I have google tracking on all pages but on my landing page when people come back from their transaction at paypal I dont know what script to use on the landing page so that it pulls the paypal pdt data and takes the info to track transaction data in google analytics. Any help. Ive looked everywhere. I converted to the new universal standard.