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!