Title: Google ecommerce tracking code
Last modified: August 20, 2016

---

# Google ecommerce tracking code

 *  [FGoldwyn](https://wordpress.org/support/users/fgoldwyn/)
 * (@fgoldwyn)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/google-ecommerce-tracking-code/)
 * So dear reader, I return again with another chapter of “The Torments of a Website
   Owner”. Today I write so as to learn and to share with others who might stumble
   on to this path. May they be spared the pain I have felt, and be calmed by the
   knowledge, however imperfect, I have gathered.
 * Let me begin by letting you know that I read from the WP-eCommerce version 3.8.8,
   which is wrapped in theme Twenty Eleven, and Twenty Eleven Child. This supports
   the noble site of [http://www.greenhillsoaps.com](http://www.greenhillsoaps.com).
 * In my wanderings, I learned that it was important to track who was wandering 
   through the noble site and which pages they were reading. so I traveled to the
   Google and was told to find a place at the Webmaster Tools and register the noble
   site there, and this I did and verified it as well. And I felt good and capable
   and wise.
 * Then a voice from the Google said that my understanding was shallow and wanting
   and that I needed to analyze what I knew. And I was sent to the Analytics of 
   the Google to register the noble work again. Then the Goolge said nay, I was 
   not yet done, but had to connect the Analytics of the Google to the Tools of 
   the Webmaster, and this I did.
 * Then the voice from the Google told me I was stupid and thick and dim witted 
   as I had not provided the Google with the means to get the information it wanted
   from the noble site. The voice directed me to my profile in the Analytics of 
   Google and told me to read the chapter on tracking code, a chapter of great mystery
   and deep knowledge. This I did, and I learned, and the Google asked how I use
   my noble site and it told me to make a choice. Choose between two kinds of tracking
   code, asynchronous and standard. But I was careful and read the text carefully,
   and there hidden in parentheses next to the asynchronous was the word “recommended”.
   So I was wise and chose that option.
 * The Google then gave me the sacred code which I copied and went back to the noble
   work and created a file called “analyticstracking.php” and entered the code there.
   Then in the body of each page of the noble work, in accordance with the instructions
   handed down from the Google, I entered <?php include_once(“analyticstracking.
   php”) ?>. Then I waited.
 * I check the Tools of the Webmaster daily. I checked the analysis of the Google
   daily. Then there was data, as though by some deep and wonderful magic. And again
   I felt good.
 * However, as time passed it became apparent that the Google has mysterious ways
   to gleen other information from the noble site and this information was also 
   important information. The noble site is also and eCommerce site where wonderful
   things are sold. So I went back to the Google and read the book of the Analytics
   again, I read the commentary by the wise Googlers and pondered it deeply. I went
   to the great Codex of Word Press, the dark forums of GetShopped and contemplated
   what I learned. Then I ventured into the vastness of the Search of Google. There
   I learned more.
 * What I understood is that if one labors on a noble site, be it web or blog, that
   the Tracking Code of the Google will gather information important to understanding
   how readers interact with a noble site. And to place the sacred code on a noble
   site, there are secret tools that do this and they are called Plugins. In particular,
   the great wise wizard Yoast has one such [plugin](http://wordpress.org/extend/plugins/google-analytics-for-wordpress/),
   as do the wise men [Visser and Leewillis](http://wordpress.org/extend/plugins/google-analytics-e-commerce-tracking-for-wp-e-commerce/).
 * However, if one also conducts commercial activities on a noble site, then this
   code is not enough, so says the Google, and it is always right. The Google says
   that there is additional code that must be on a noble site that tracks eCommerce
   and tells the Google about what people take away from the noble site and how 
   they take it. And with this additional code the Google can gleen truly amazing
   knowledge about a noble site and its visitors. Again I am left in awe of the 
   Google and forced to face my wretchedness.
 * So I returned to the Google and the Analytics of the Google and in my profile
   I went to the profile settings. There I shamefully admitted to the Google that
   the noble site was in fact an eCommerce site and that I had erred in not telling
   the Google before, and begged the Google’s forgiveness. And the Google was kind
   and told me about the secret [link](https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce).
 * Be patient with me dear reader for the story I have told so far is long and full
   of frustrations, but now we delve into real magic, for now we have been given
   the Script of Java, a strange and mysterious language full of runes, symbols,
   and great confusion.
 * As I read through the Tracking of Ecommerce, I found three strange markings, 
   _addTrans(), _addItem(), and _trackTrans(). These remarkable writings I had not
   seen before, they were not in the previous tracking code given to me by the Google.
   So carefully I reread the Tracking of Ecommerce and contemplated the meaning 
   of these new incantations.
 * I discovered that _addTrans() is what the Google calls a “Transaction Object”.
   This object stores all the information about a transaction. Deep within this 
   object is an obscure magical instruction called an “order ID” or “ID”. This is
   important as this information is what binds all the other information in the 
   object together in a unique way. Only information that are part of the order 
   ID can be in a transaction object. There can be many transaction objects, but
   each is unique as defined by the order ID. I sat back, stunned and amazed by 
   the brilliance of the Googlers and the Google. There is more.
 * I discovered that within transactions there are items that make up the transactions.
   The Google needs to collect these items within each transaction. The incantation
   _addItem()does exactly this.
 * But I thought to myself, not all transaction are completed and not all items 
   in a transaction are bought. So how does the Google know what is a transaction
   and what is a completed transaction? A voice rang out from the Google and reprimanded
   me for being alive and thinking I was more than an apple or a mouse. I was ashamed
   yet again.
 * Then I realized that _trackTrans()was the incantation that told the Google if
   the transaction object was passed all the way through to checkout and should 
   be treated as a completed transaction.
 * But how are these incantations used? Again I had to choose between asynchronous
   and standard. Again I was careful and read the text and found the parentheses
   containing the word “recommended” next to the word asynchronous. So I was wise
   and chose that option.
 * The room glowed and I was shown the following:
 *     ```
       <html>
       <head>
       <title>Receipt for your clothing purchase from Acme Clothing</title>
       <script type="text/javascript">
   
         var _gaq = _gaq || [];
         _gaq.push(['_setAccount', 'UA-XXXXX-X']);
         _gaq.push(['_trackPageview']);
         _gaq.push(['_addTrans',
           '1234',           // order ID - required
           'Acme Clothing',  // affiliation or store name
           '11.99',          // total - required
           '1.29',           // tax
           '5',              // shipping
           'San Jose',       // city
           'California',     // state or province
           'USA'             // 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
         _gaq.push(['_addItem',
           '1234',           // order ID - required
           'DD44',           // SKU/code - required
           'T-Shirt',        // product name
           'Green Medium',   // category or variation
           '11.99',          // unit price - required
           '1'               // quantity - required
         ]);
         _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
   
         (function() {
           var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
           ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
           var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
         })();
   
       </script>
       </head>
       <body>
   
         Thank you for your order.  You will receive an email containing all your order details.
   
       </body>
       </html>
       ```
   
 * This is Script of Java, that can be placed in your noble site. The first three
   lines of the Script of Java I remembered from the first tracking code.
 * The line `var _gaq = _gaq || []` defines the variable gaq so that the instructions
   _gaq.push never [fail](http://stackoverflow.com/questions/2538252/what-is-var-gaq-gaq-for).
 * The lines with `_gaq.push` contain a Script of Java function that tells the Script
   of Java to get an object and push it to the Google. It is called a “syntax” of
   the Script of Java used by the Analytics of Google Asynchronous call. It could
   be roughly translated as “add the function (name) _setAccount onto a queue, and
   then fire all of the functions in that queue sequentially once ga.js has loaded.
   It allows you to safely load ga.js [asynchronously](http://stackoverflow.com/questions/6166074/how-does-gaq-push-trackpageloadtime-work).
 * In the first line, is the function ‘_setAccount’ followed by the element UA identifier.
   This allows the Google to know that the data is from you noble site and not that
   of the Soft of Micro or the Book of Face.
 * The The third line is the function_gaq.push([ ‘_addTrans’,”.”…]). This is the
   transaction object I mentioned above, and is followed by what the Script of Java
   calls an “Array”. The Array is made up of the individual “Elements” beginning
   with:
 *     ```
       '1234',           // order ID - required
       ```
   
 * Below the first Script of Java Array is the second Script of Java Array called
   _addItem() which is then followed by the function
 *     ```
       _gaq.push(['_trackTrans']);
       ```
   
 * This is then followed by the same Script of Java that was used in the first tracking
   code.
 * But gentle reader, do not think that this is the end of our mystery or our torment.
   No, we need to understand the elements of each array for here is the real magic,
   here is the deep information that the Google want to know, track and understand.
 * And it is here that I again fell into the pit of eternal ignorance. For you see,
   in my haste I copied the Script of Java and pasted it, as given to me by the 
   Google, into my noble site. In particular, I pasted it into my “analyticstracking.
   php” file which the Google suggested I create.
 * Again, I checked the Tools of the Webmaster daily. Again, I checked the Analysis
   of the Google daily. Then there was data. Again it was as though by some deep
   and wonderful magic. I looked at the data, I studied the data, and it was not
   as it was supposed to be.
 * My orderID’s were all 1234. My affiliation was Acme Clothing, my totals were 
   all 11.90, my city was always San Jose. No, this was not what was supposed to
   be, so I wept, I tore my garments, I screamed my pain to the universe; and the
   Google laughed.
 * Oh the Google laughed at my ignorance and lack of understanding. I was clearly
   too simple to understand that the code was an “example”, it must have been so,
   for it did not work as the Google said it should, and the Google is always right.
 * That night, I calmed myself with many tipples of bourbon, an fed my weary body
   and soul with rare meat. I took to my bed still angry at my ignorance. When I
   woke, I found a new determination to do two things, cure my pounding head and
   fix the code of the Google.
 * First, I looked at what I put in my file:
 *     ```
       <script type="text/javascript">
   
         var _gaq = _gaq || [];
         _gaq.push(['_setAccount', 'UA-29921530-1']);
         _gaq.push(['_setDomainName', '.greenhillsoaps.com']);
         _gaq.push(['_setCookiePath', '/soap-shop/']);
         _gaq.push(['_trackPageview']);
         _gaq.push(['_CookiePathCopy', '/checkout/']);
         _gaq.push(['_addTrans',
           '1234',           // order ID - required
           'Acme Clothing',  // affiliation or store name
           '11.99',          // total - required
           '1.29',           // tax
           '5',              // shipping
           'San Jose',       // city
           'California',     // state or province
           'USA'             // 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
         _gaq.push(['_addItem',
           '1234',           // order ID - required
           'DD44',           // SKU/code - required
           'T-Shirt',        // product name
           'Green Medium',   // category or variation
           '11.99',          // unit price - required
           '1'               // quantity - required
         ]);
         _gaq.push(['_trackTrans']);
   
         (function() {
           var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
           ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
           var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
         })();
   
       </script>
       ```
   
 * The Google had advised that I place a “cookie” where I want the tracking to begin.
   So I entered the function:
 *     ```
       _gaq.push(['_setCookiePath', '/soap-shop/']);
       ```
   
 * I then added the function:
 *     ```
       _gaq.push(['_CookiePathCopy', '/checkout/']);
       ```
   
 * because the Google said “When this function is called, it immediately copies 
   any cookies to the new cookie path. Therefore, if the _trackPageview() method
   has not been called, the values for any existing Analytics cookies will be supplied.
   For that reason, we recommend that this method is called after _trackPageview()
   as well as other configuration methods that might impact cookie values or campaign
   values that you want to pass [along](https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._cookiePathCopy)“
 * The code from the Google clearly worked, but was sending fixed information. How
   could I change this to display the information that was important? Where would
   that information come from? How do I find it? Is the world only darkness and 
   gloom?
 * So I returned to the vast Search of the Google, I returned to the dark forums
   of Getshopped.org, and I studied the Codex of WordPress. In my blindness I suddenly
   saw a twinkle of light. I came across a noble site run by the majestic Ben Huson.
   In his note, he uses different elements for the code of [Google!](http://www.benhuson.co.uk/2010/08/11/google-analytics-wpecommerce-transactions/#more-1673)
 * My pulse quickened and I plunged back into more research. Tucked away in the 
   corner of the Forum of WordPress I found a note by SZYM9341 in which he notes
   his struggles and tribulations with the Tracking Code of the [Google](http://wordpress.org/support/topic/google-ecommerce-not-tracking-sales?replies=15).
   Others also commented on his note. As I read the comments and empathized with
   the writers, I came across a note by the incomparable CJ3Wilso. She had figured
   out how to properly code the Script of Java. The gift she gave was an example
   of her Script of Java.
 * I read the note and her incantations then realized that some of her runes were
   runes of PHP. I went to the tome on PHP and looked in the Base of the Data for
   my noble site, and there I found the same runes she had used in her Script of
   Java.
 * I copied her code and checked the references to my runes of PHP in the Base of
   Data for my noble site. They were the same and seem correct.
 * Now my Tracking Code of the Google reads as follows:
 *     ```
       <script type="text/javascript">
   
         var _gaq = _gaq || [];
         _gaq.push(['_setAccount', 'UA-29921530-1']);
         _gaq.push(['_setDomainName', '.greenhillsoaps.com']);
         _gaq.push(['_setCookiePath', '/soap-shop/']);
         _gaq.push(['_trackPageview']);
         _gaq.push(['_CookiePathCopy', '/checkout/']);
         _gaq.push(['_addTrans',
       '<?php echo $purchase_log['id']; ?>', // order ID - required
       '<?php echo get_bloginfo('name'); ?>', // affiliation or store name
       '<?php echo $purchase_log['totalprice']; ?>', // total - required
       '<?php echo $purchase_log['wpec_taxes_total']; ?>', // tax
       '<?php echo $purchase_log['base_shipping']; ?>', // shipping
       '<?php echo 'NA'; ?>', // city
       '<?php echo wpsc_get_region( $purchase_log['billing_region'] ); ?>', // state or province
       '<?php echo wpsc_get_country( $purchase_log['billing_country'] ); ?>' // country
         ]);
        _gaq.push(['_addItem',
       '<?php echo $purchase_log['id']; ?>', // order ID - required
       '<?php echo $sku['meta_value']; ?>', // SKU/code - required
       '<?php echo $purchase_log_data_item['name']; ?>', // product name
       '<?php echo $category['name']; ?>', // category or variation
       '<?php echo $purchase_log_data_item['price']; ?>', // unit price - required
       '<?php echo $purchase_log_data_item['quantity']; ?>' // quantity - required
           ]);
         _gaq.push(['_trackTrans']);
   
         (function() {
           var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
           ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
           var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
         })();
   
       </script>
       ```
   
 * I have this posted in the same analyticstracking.php file as before. However,
   the wise CJ3Wilso says to put it in the wpsc-transaction_results.php file at 
   the bottom of the file. So I copied that file to my Theme Twenty Eleven Child
   and posted the Script of Java to that new file.
 * So weary reader, now we wait. If any of you find fault or error in what I have
   written or done, please be not mad but guide me and others like me out of the
   darkness. If you find the depth of our understanding to be lacking, then let 
   us drink from your cup of knowledge.
 * I, for one, do know know if having the Tracking Code of the Google in two places
   is correct. I believe I have entered all the code correctly and carefully. But
   I have been wrong before. I have learned the painful lesson that leaving out 
   semicolons, wiggly brackets, forward slashes or other symbols can turn you into
   a frog. And I can testify that it is much easier to be turned into a frog than
   to turn oneself back out of a frog.
 * As the wise monks say: “code may be poetry, but write it carefully”
 * I await your comments and advice.

The topic ‘Google ecommerce tracking code’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [FGoldwyn](https://wordpress.org/support/users/fgoldwyn/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/google-ecommerce-tracking-code/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
