Wednesday, June 24, 2009

Netsuite and Google Analytics How To - Part 3

These last few steps are going to include several "customizations" to your site (perhaps you already have them) and a little bit of code work.
So lets start with a high level discussion and then dive right into the details.
The way I implemented a solution was through the creation of a custom Web Site Tag, inclusion of my "magic" code in the tag and then the addition of the tag to the appropriate page.
VERY IMPORTANT NOTE: There is a huge assumption if you just follow this without thinking about your implementation - and that assumption is - your site is the same as mine...
First off you will be making some changes inside Lists > Web Site > Content Manager to the "Checkout" page - this is where the code I'll provide will reside to populate the order information back into Google Analytics.


I have tried to get the code to work correctly by using the Setup > Web Site Setup > Set Up Website > Shopping tab - in the "Order Confirmation Page"... but I had problems with the positioning on the page and some other issues that going with my FOOTER tag re-write made this work for me. You may find due to the design of your own "Thanks for ordering page" you can plop the code I provided into this box and have good results. If your site does not have a FOOTER tag that you can easily replace this with your own tag that gets assigned to the checkout pages only - and has the code.
So - without further adou - here's the code that constitutes the order collection slice for Netsuite to populate Google Analytics:

<!-- Start GA Checkout Code -->

<script type='text/javascript'>

if(document.title == 'Thanks - COMPANY NAME') {

document.getElementsByClassName = function(cl) {
var thing = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) thing.push(elem[i]);
}
return thing;
};

myBodyElements = document.getElementsByClassName("texttable");
myP = myBodyElements[0];
var sku = myP.firstChild.data;
myBodyElements = document.getElementsByClassName("texttablectr");
myP = myBodyElements[0];
var qty = myP.firstChild.data;
myBodyElements = document.getElementsByClassName("texttablert");
myP = myBodyElements[1];
var unitPrice = myP.firstChild.data;
myBodyElements = document.getElementsByClassName("bglttext");
myP = myBodyElements[0];
var asOptions = myP.firstChild.data;

var str="<%=getCurrentAttribute('customer','defaultaddress')%>";
var lastBr = str.lastIndexOf("<br>");
var firstBr = str.lastIndexOf("<br>",lastBr - 1);
var country = str.substring(lastBr+4,str.length);
var splitUp = str.substring(firstBr+4,lastBr);
var splitUp2 = splitUp.split(" ");
var city = splitUp2[0];
var state = splitUp2[1];
var ordernumber = "<%=getCurrentAttribute('confirmation','ordernumber')%>";
var promotioncoderef = "<%=getCurrentAttribute('confirmation','promotioncoderef')%>";
var total = "<%=getCurrentAttribute('confirmation','total')%>";
var tax = "<%=getCurrentAttribute('confirmation','tax')%>";
var shipping = "<%=getCurrentAttribute('confirmation','shipping')%>";

var pageTracker = _gat._getTracker("UA-xxxxxxx-x");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._initData();
pageTracker._trackPageview("/thanks");
pageTracker._addTrans(
ordernumber, // Order ID
promotioncoderef, // Affiliation
total, // Total
tax, // Tax
shipping, // Shipping
city, // City
state, // State
country // Country
);

pageTracker._addItem(

ordernumber, // Order ID
sku, // SKU
sku, // Product Name
asOptions, // Category
unitPrice, // Price
qty // Quantity
);

pageTracker._trackTrans();
}

</script>

<!-- End GA Checkout Code -->

The two obvious changes you need to make to this code are in red - the <TITLE> of YOUR Order Confirmation page (so this script only runs on that page during the checkout process)... and the addition of your GA code. REMEMBER - I use this in a Tag Substitution manner - if your cart design allows you to place it on the order confirmation page - go for it - but that's a challenge since the script is actually doing some basic screen scrapping and needs to be AFTER the values are populated on the screen.
The script can be pretty easily modified to pull different information (like partner) if you want that. But there is one limitation to this script... I have never been able to get it to successfully iterate over multiple contents in the cart... meaning - this script will allow you to tie orders and conversion to geography, campaigns, products, etc... the TOTAL of the cart transaction will be recorded. The shipping and tax will be recorded... and the LAST product in the cart will be recorded... but not every item (Netsuite can tell you that with reporting though) Iteration through the cart and the addition of multiple "pageTracker._addItem" elements to Google Analytics is something I have not been successful at solving.
To offer some guidance on this for the more ambitious - you need to loop through the cart items and assign them to the Google addItem object...
You may also look at this and say "wait a minute - why are some of the categories being populated in Google Analytics with the same information, like sku" - which is a great question... the answer is - this is what I needed for my implementation. The nice thing here is - the script is very open ended and you should be able to play with both the data you want stuffed into Google as well as the <%=getCurrentAttribute('confirmation','WHATEVER')%> tags Netsuite provides with special content on the Confirmation page.
That's it - pretty simple when you look at it. Good luck!

11 comments:

  1. Andy,

    Been waiting for this!!! Thanks so much for sharing it!

    Question about implementation, tho.

    I'm a little confused as to where you actually put the code. Here's what I interpreted and tried...

    - I created a tag called GACHECKOUT and left it's default content blank.
    - Then I inserted the tag at the end of my footer in my Theme.
    - Then I went to Web Site Tabs> Checkout tab> and, at the bottom, selected the GACHECKOUT tag and input your code as the substitution value. (I changed the title and GA account code so they are correct for our website.)

    Is that what you meant to do?

    I have not tested to see if Google Analytics is reading the code yet, but I did discover one issue so far. I viewed our checkout confirmation page in IE and it said there was an error in this line of the code:
    myP = myBodyElements[0];
    It said that 'myP.firstChild' is null or not an object.

    Any ideas what that means or what I might have done wrong?

    Thanks again... mimi

    ReplyDelete
  2. I just added the code to the footer, as it will only fire when the title is correct.

    Alas myP is also undefined...

    ReplyDelete
  3. What are you harvesting from bglttext?

    That is where we are getting the error...

    It does not error if I remove that!

    Not sure if it is tracking yet, or what info i have lost with bglttext

    ReplyDelete
  4. Ahh yes
    - oops!

    This block of code:
    myBodyElements = document.getElementsByClassName("bglttext");
    myP = myBodyElements[0];
    var asOptions = myP.firstChild.data;

    scrapes elements from my "Netsuite item Options" and pulls that items options into the record in Google (as my products have them and they are configured to DISPLAY in the checkout)

    The fix is to look at the HTML elements on your page to determine IF you have other things displayed that you want to pull into the Google record of the sale - and then substitute the element for "bglttext".

    You other option is to rip that block of code out and then change the "asOptions" variable to something else - or hard code it...

    That should fix the issues you all see... sorry... nothing worse than waiting for an answer - only to have it stink on ice.

    ReplyDelete
  5. Haven't got it going quite yet...

    All my revenue is still be reported to be from (direct) ((none))

    Does yours show the referrer?

    ReplyDelete
  6. @plantraco - Yes it does... check the code you have from Part 2.
    The GA code itself that needs to be displayed on every page needs those 2 lines of red added for sure... and then you also have to make sure the code is accurate in the submit button.
    Then use something like Firebug in Firefox to make sure the "Proceed to Checkout" button shows the code on it - AND - all pages in the cart have the GA code on them.
    A really easy test to see if things are working right is to look at the URL right after the "Proceed to Checkout" button is clicked and you hop to the Netsuite domain... you should see a TON of GET variables passed on the URL - including all the Google ones... when you see that - you have it working.

    ReplyDelete
  7. I got the loop running!!! All items in the order is getting reported...

    I just need to clean up the code and add some comments.

    ReplyDelete
  8. So I have been trying to post the code that loops all the products, so that they are all reported in GA. But Blogger will not let me post any code in these comment windows.

    Send me an email if you need it.

    http://www.microflight.com/Contact-Us

    Andy, email me and than you can post it up here too.

    And all I want in return is a shameless plug:
    http://www.microflight.com/Micro-Spitfire-Video

    Jason

    ReplyDelete
  9. also, geo-location data is no longer used by Google Analytics. The new version of GA tries to identify where the buyer is located using an IP address lookup.

    So I pulled that code too.

    ReplyDelete
  10. This is a very nice post, I was looking for something like this, actually I am new in NetSuite industry that's why I am looking for more and more guides on the web so I can gain knowledge about Netsuite CRM. Thank you so much for sharing such a nice post this helped me a lot.

    ReplyDelete
  11. NetSuite is a totally effective ERP system, however it is not continually perfect for every enterprise. I can help you customize NetSuite to satisfy your precise needs, whether or not you need to add new capabilities or alter current ones.
    San Francisco NetSuite Expert

    ReplyDelete