uc_googleanalytics doesn't work with Latest GA Tracking code! (Fix attached)

43 replies [Last post]
Joined: 08/14/2007
Juice: 3913

After changing my Google Analytics module to use the latest & greatest code, I noticed that our visits improved, but our Goals and Ecommerce reports went down. After further investigation I realized that the uc_googleanalytics module is using the old (Legacy) version of the tracking code from Google.

So I've added conditionals that check for whether Legacy is selected in the Google Analytics Drupal modue (googleanalytics.module) and if it's set to TRUE uses the normal Legacy code; otherwise it will use the new format for assembling the Transaction and Item code. (They are arrays now).

PLEASE NOTE: I've just installed this today and I'm waiting to see how our reports do (and especially if they come back). So it should be noted that, while I can confirm the code itself works, I'm waiting to see if it has the positive impact on our Analytics as I'm hoping it will. Even so I figured I would attach my module, so that others can test as well - especially if they run into the same problem I did.

UPDATE: Please scroll down and read this entire thread. My changes in this initial patch did not seem to work correctly, and others have contributed untested code to try and get this working with Ubercart.

Hope this helps!

Old:

<form style="display:none;" name="utmform">
<textarea id="utmtrans">UTM:T|[order-id]|[affiliation]|
[total]|[tax]| [shipping]|[city]|[state]|[country] UTM:I|[order-id]|[sku/code]|[productname]|[category]|[price]|
[quantity] </textarea>
</form>

New:

<script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-XXXXX-1");
  pageTracker._initData();
  pageTracker._trackPageview();

  pageTracker._addTrans(
    "1234",                                     // Order ID
    "Mountain View",                            // Affiliation
    "11.99",                                    // Total
    "1.29",                                     // Tax
    "5",                                        // Shipping
    "San Jose",                                 // City
    "California",                               // State
    "USA"                                       // Country
  );

  pageTracker._addItem(
    "1234",                                     // Order ID
    "DD44",                                     // SKU
    "T-Shirt",                                  // Product Name
    "Green Medium",                             // Category
    "11.99",                                    // Price
    "1"                                         // Quantity
  );

  pageTracker._trackTrans();
</script>

PreviewAttachmentSize
uc_googleanalytics-1.1.tar.gz2.47 KB

Help directly fund development: Donate via PayPal!

Joined: 08/14/2007
Juice: 3913

Results not showing up, realized I was missing a tag. Attached is new version. Will be testing this and checking again tomorrow.

AttachmentSize
uc_googleanalytics-1.2.tar.gz 2.5 KB

Help directly fund development: Donate via PayPal!

Joined: 08/14/2007
Juice: 3913

Well this code didn't fix anything. My only guess is that it's because I had to enclose the transaction script in new tags. I'm not sure how to get it to be combined with the ga.js code that's normally found at the bottom of the html source.

The current uc_googleanalytics module (using the Legacy code) puts the transaction information into a form. The new version of the code doesn't require this. It only requires that you add calls to _addTrans, _addItem, and _trackTrans but these have to be appended to the regular call to the ga.js tracking script.

If anyone has any ideas, please let me know. Otherwise I'll just switch the site back to Legacy code I guess. (Bummer)

EDIT: Complete example from Google.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-XXXXX-1");
  pageTracker._initData();
  pageTracker._trackPageview();

  pageTracker._addTrans(
    "1234",                                     // Order ID
    "Mountain View",                            // Affiliation
    "11.99",                                    // Total
    "1.29",                                     // Tax
    "5",                                        // Shipping
    "San Jose",                                 // City
    "California",                               // State
    "USA"                                       // Country
  );

  pageTracker._addItem(
    "1234",                                     // Order ID
    "DD44",                                     // SKU
    "T-Shirt",                                  // Product Name
    "Green Medium",                             // Category
    "11.99",                                    // Price
    "1"                                         // Quantity
  );

  pageTracker._trackTrans();
</script>

EDIT EDIT: I may have found that I had extra commas at the end... I know this can sometimes break JS so I am testing again. Will update tomorrow.

Help directly fund development: Donate via PayPal!

Joined: 08/07/2007
Juice: 15375

Thanks for your work here. Once it's good to go I'll roll changes into core. Will your changes preserve backwards compatibility?

Joined: 08/14/2007
Juice: 3913

Yeah, all the changes should preserve anything so that if you decide to go back to the Legacy code, which works in Ubercart right now, all the correct JS will show up in the footer.

I'm still waiting to see if taking out my extra comma fixed the javascript.. so far we have no Ecommerce conversions for the last 5 days... argh. At least we have the Reports module to use Smiling

Help directly fund development: Donate via PayPal!

Joined: 08/08/2007
Juice: 179

Did you get it to work?

It only requires that you add calls to _addTrans, _addItem, and _trackTrans but these have to be appended to the regular call to the ga.js tracking script.
This shouldn't really be a problem?
Can't you just do like this for ordinary pages (this is what I guess you already do):

<script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-XXXXX-1");
  pageTracker._initData();
  pageTracker._trackPageview();
</script>

Then, when you hit the order complete page, you just append this at the end of page (just like how old legacy form is appended to the end of the page). The pageTracker variable is already declared in the previous javascript code:

<script type="text/javascript">
  pageTracker._addTrans(
    "1234",                                     // Order ID
    "Mountain View",                            // Affiliation
    "11.99",                                    // Total
    "1.29",                                     // Tax
    "5",                                        // Shipping
    "San Jose",                                 // City
    "California",                               // State
    "USA"                                       // Country
  );

  pageTracker._addItem(
    "1234",                                     // Order ID
    "DD44",                                     // SKU
    "T-Shirt",                                  // Product Name
    "Green Medium",                             // Category
    "11.99",                                    // Price
    "1"                                         // Quantity
  );

  pageTracker._trackTrans();
</script>

Erlend Strømsvik
Ny Media AS
erlend@nymedia.no

Joined: 08/14/2007
Juice: 3913

Ah ha.. I think I missed putting the call to trackTrans() in every footer. I just updated the code on my live site, if it works then I will post the finished contrib (actually I'll just post a patch).

Thanks for the tip Smiling

EDIT: Actually it doesn't look like that's the case. One reason I might not be seeing reports yet is because of my subdomain filter. Time to play around some more...

(A side note: it probably doesn't matter that the two scripts are in different <script>tags, does it?)

Help directly fund development: Donate via PayPal!

Joined: 08/14/2007
Juice: 3913

No dice. Still testing, not sure what else I could be doing wrong. I have pored over the documentation for the API, and it does look like it's setup correctly; the only exception being that the main tracking code is kept within a separate pair of <script> tags than the ecommerce tracking code. This is just because the googleanalytics.module outputs the script has a string variable... there is no global function or hook to invoke other google modules and combine the code from them all at one time.

That would be ideal, IMO - but it'd require patching the current googleanalytics.module.

Help directly fund development: Donate via PayPal!

Joined: 03/14/2008
Juice: 7

Thanks for taking up the effort torgosPizza. I did some more investigation in this using Firefox' Live HTTP Headers extension. After removing the trailing commas the pageTracker._addTrans() definitely fired, i can see the data being transferred to http://www.google-analytics.com/__utm.gif. So there is no issue with the separate tags (and there should not be anyway).

Whether the data sent can be handled correctly by Google, I do not know. So far, I can not see any transactions. Do I have to define some goals for this in Analytics or is it sufficient to mark your site as an e-commerce site? Does the data show up immediately in Analytics? Sorry for these questions, I am new to Analytics.

While digging into this, I noticed an undefined call to __utmSetTrans() in the JS error console. This error however, I do not believe causes any problem, it is just an annoyance. To get rid of it I propose this patch:

--- uc_googleanalytics.module.bak       2008-03-14 01:40:25.613947962 -0700
+++ uc_googleanalytics.module   2008-03-14 01:40:38.114660337 -0700
@@ -36,7 +36,9 @@
       $script .= _uc_googleanalytics_ecommerce_form($order);
       $_SESSION['uc_googleanalytics_order_id'] = NULL;
       unset($_SESSION['uc_googleanalytics_order_id']);
-      drupal_add_js('$(document).ready(function() { __utmSetTrans(); });', 'inline');
+      IF (VARIABLE_GET('googleanalytics_legacy_version', TRUE) == TRUE) {
+          drupal_add_js('$(document).ready(function() { __utmSetTrans(); });', 'inline');
+      }
     }
   }
   return $script;

If anyone can comment on my questions about how Analytics and e-Commerce Tracking works, let me know so I can dig further into getting GA tracking work with Ubercart.

Joined: 08/14/2007
Juice: 3913

Thanks for the patch, I had noticed this too and fixed it in my working version (but not uploaded it yet).

Unfortunately I'm still not seeing anything in Analytics. Although I might give it one more day. It does take 24 hours for data to start being reported in the Dashboard. You don't need to define any goals but you do need to mark your site as being an Ecommerce site. I think after that you should be fine.

I want to keep testing this but we also need our Analytics data, so I'm considering moving back to Legacy code. It might be some extra stuff I have setup with my site (subdomains and filters) so I will look into that today.

EDIT: Looks like I AM receiving data! It's only a handful of transactions but I'm going to wait and see tomorrow, since we have a new product release today. The results I'm seeing in GA are from 2 days ago (thanks to the 24 hour delay) so tomorrow will let me know if the code I've created still works. I will let you know here.

Help directly fund development: Donate via PayPal!

Joined: 03/14/2008
Juice: 7

I can also confirm that data is being received by Analytics, it shows up in my account now.

Joined: 08/14/2007
Juice: 3913

Great! I will update over the weekend if my conversions are working correctly. It looks like ECommerce is being tracked now in GA, and if that's the case, I will commit my patch for the Uberdudes.

Help directly fund development: Donate via PayPal!

Joined: 08/07/2007
Juice: 15375

Rock on. Just say the word and I'll put it up for testing on the Livetest.

Joined: 01/18/2008
Juice: 231
Joined: 08/14/2007
Juice: 3913

Yeah but I am still testing it. I was having issues with our live site and so it needs to go through some more hazing to make sure my changes work.

Help directly fund development: Donate via PayPal!

Joined: 09/28/2007
Juice: 103

Hey guys,

I'm having the same issue of no Ecommerce reports in GA. I am also using the GA tracker over the urchin.

Any updates with this? Is the latest version safe to use yet? Are you still receiving data?

Using:

Drupal 5.7
Ubercart 5.x-1.0-rc4
Google Analytics 5.x-1.4

Let me know when you get a chance.

Thanks!

txcrew

Joined: 08/14/2007
Juice: 3913

Hi,

Unfortunately I was never able to get GA stats working with the new code. I don't know why - everything looked correct. And doubly unfortunate, our CEO was getting anxious that we weren't receiving Conversion Data - so I had to revert back to the Legacy code. If I can convince him that we need the new code features, I will try to troubleshoot it again.

For now, though, the code isn't safe if you are requiring conversion and Ecommerce data. If you're not, then feel free to use it. I would love it if someone else could take a look and try to fix the issues. I've run out of time to look at this, for now. The thing that sucks is that you don't know if the fixes you have made are actual fixes until the next day!

Sorry about that. If I get free time (and can convince the CEO as I mentioned) I will try to take another crack at it.

Help directly fund development: Donate via PayPal!

Joined: 09/28/2007
Juice: 103

Thanks for the reply. I too reverted back to the Legacy code. Everything is working great now!

- txcrew

Joined: 03/07/2008
Juice: 75

Have you figured this issue out? We are using uc_googleanalytics 5.x-1.0 and we are using the latest ga.js code. Do we need to use the legacy code for now?

Joined: 08/14/2007
Juice: 3913

I did not, but someone else had, I think. I haven't gotten to testing it yet, though.

http://drupal.org/node/280090

Help directly fund development: Donate via PayPal!

Joined: 08/12/2008
Juice: 50

I've followed the link you provided and it's still not clear if UC supports the latest GA code. The UC install page refers to the GA module 5.X-1.5 and indicates that we must use the legacy option. The current version of the GA module is 5.X-1.6, where the new GA code is enabled by default. So does using the latest module and new GA code along with UC work? TIA.

Joined: 08/14/2007
Juice: 3913

I can't tell because I haven't updated yet. I think I read positive results somewhere around here though, in another thread. Feel free to test it out and post your results.

Help directly fund development: Donate via PayPal!

Joined: 11/26/2008
Juice: 4

So is there any definitive word on this issue yet?

Can we use the ga.js code, or should I continue using the legacy code?

thanks

Joined: 08/14/2007
Juice: 3913

I'd like to know as well - I'm currently still using Legacy code since we have too many visitors (and right now is a crucial time for us) to be experimenting with code that might not work. If anyone is using the new GA code please fill us in Smiling

Help directly fund development: Donate via PayPal!

Joined: 08/22/2008
Juice: 416

I tried testing the code (after a 24 hour wait, of course Eye-wink and it seemed like it was sendin stats, but I didn't see anything sent for orders or anything. Where do you find that on the google analytic UI?

Also, I was wondering where you picked up how to create the new GA JS... I've done some looking around for info on how to interface with GA but I'm not coming up with much... If you could point me towards some resource(s) to read, I would be mucho grateful-o...

Try FreeBASIC!
My game Lynn's Legacy

Joined: 08/14/2007
Juice: 3913

Make sure the site is setup as an Ecommerce site first in GA, and then you'll start seeing commerce information, such as order amounts. I think it's just under the normal Account settings for your particular tracking code.

I picked up the GA.js code format from the Google page itself, there are some help documents that I found useful.

For e-commerce: http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer...

Installation overview: https://www.google.com/support/googleanalytics/bin/answer.py?answer=6698...

I had tried following the e-commerce guide as well as their example code but that didn't seem to do any good. I'm pretty sure I still have the new code I wrote (including the patch that was figured out above) but I haven't gotten to test it yet. Since it's the holiday season I'm not sure I want to play with it on our current site, but I could run it by the CEO to get his thoughts.

Help directly fund development: Donate via PayPal!

Joined: 01/25/2008
Juice: 799

I have upgraded one one site to the new GA 1.7 Module and running 1.6 Ubercart with no loss of any ecommerce data. Everything seems to be flowing like normal after 3 days of use.

Is there anything specific I should be watching for?

Joined: 08/14/2007
Juice: 3913

The main question at this point is to make sure Ecommerce data is being received and updated (such as sales totals, order amounts, etc.) If you're seeing this information, then I might be willing to give the code another test run.

Which code is it that you're running on your server? Keep in mind we're talking about using the new ga.js code, not the Legacy (urchin.js).

Help directly fund development: Donate via PayPal!

Joined: 01/25/2008
Juice: 799

I am using the ga.js I see all the orders and products and such in the ecommerce section. I didn't do anything with the patch above. I just upgraded to 1.7 3-4 days ago, and am using the stock ubercart google analytics that is with ubercart 1.6. This is the code on the bottom of the pages when I do a view source.

<script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
<script type="text/javascript">var pageTracker = _gat._getTracker("UA-*******-1");pageTracker._trackPageview();</script>
AttachmentSize
Google Analytics .jpg 271.21 KB
Joined: 08/14/2007
Juice: 3913

Interesting. So, if you were to perform a test checkout on your site, would you see all of the transaction data for your products? I'm not sure if Ryan used any of the code I provided earlier on in this thread, because I had definitely made some changes to the uc_googleanalytics file (specifically, the format of the JS had changed for the new ga.js versus the old Legacy code).

Help directly fund development: Donate via PayPal!

Joined: 01/25/2008
Juice: 799

Here are some additional screenshots, had to black out the actural figures, not this is from yesterday, when running on the ga.js code.

AttachmentSize
Ecommerce-Overview-.jpg 33.81 KB
Transactions.jpg 44.06 KB
Joined: 08/14/2007
Juice: 3913

That's good to know. Perhaps I'll give it a shot and see what happens.

Help directly fund development: Donate via PayPal!

Joined: 08/22/2008
Juice: 416
torgosPizza wrote:

Make sure the site is setup as an Ecommerce site first in GA

D'oh! That was set to 'no'. Sticking out tongue I will do another test as well and see what she tells me...

Try FreeBASIC!
My game Lynn's Legacy

Joined: 08/14/2007
Juice: 3913

Hey cha0s, any luck?

Help directly fund development: Donate via PayPal!

Joined: 01/25/2008
Juice: 799

It seems I have stumbled on a small issue.

What's happening is that the transaction total being passed to Analytics includes shipping and taxes, but should only total products. The total revenue figures are inflated by shipping & tax amounts being doubled up. Since Analytics adds shipping and tax on its own, they should not be included in the transaction total value.

Any thoughts?

EDIT: was going to show an example, but it was an older order before the upgrade. It looks fine since the upgrade.

Joined: 08/14/2007
Juice: 3913

So I'm confused, is the issue you're describing no longer present in the new uc_googleanalytics module?

Help directly fund development: Donate via PayPal!

Joined: 01/25/2008
Juice: 799

I think everything is ok, I ust was a little confused when looking at the script output.

When buying item A for $14.99 and item B for $15.99 the output was the folowing:

<script type="text/javascript">pageTracker._addTrans("16193","uberdemo.com","40.83","1.86","7.99","Jackson","49203","United States");

It looked like the total was combining the tax and shipping and then adding them in again. The two products total 30.98 but i saw 40.83. I then looked in google analytics and say it was all correct.

See the screen shots for how the output shows in google analytics

AttachmentSize
Transactions - Google Analytics.jpg 150.58 KB
Transaction_ - Google Analytics.jpg 134.39 KB
Joined: 06/24/2008
Juice: 247

Is there a patch or module that works for "Latest GA Tracking code!" for UC 1.6?

Joined: 08/07/2007
Juice: 15375

As far as I know, it's working. I did update that recently, so lemme try and get a 1.7 release out today and look there. Smiling

Joined: 06/24/2008
Juice: 247

Thanks Ryan.

Looking forward for UC 1.7 with latest GA tracking code.

Joined: 04/18/2008
Juice: 79

Hi,

I came accross a bug with uc_googleanalytics.module as well. When one has a product or catagory with special chars in 'm it messes up the javascript _addItem function.

Example;

<script type="text/javascript">
pageTracker._addTrans("24", "Websop", "000.00", "0", "00.00", "Test", "Test", "Neverland");
pageTracker._addItem("24", "1234.0", "Honda Civic 24" rimms", "24" rimms", "999.00", "1");
pageTracker._trackTrans();
</script>

See the error, not the one that a Civic will never fit 24" rimms Smiling

Quotes are not escaped: "Honda Civic 24" rimms",

I believe that checking_plain will fix this:

$items .= 'pageTracker._addItem("'. check_plain($product->order_id) .'", "'
            . check_plain($product->model) .'", "'. check_plain($product->title) .'", "'. check_plain($category)
             .'", "'. check_plain($product->price) .'", "'. check_plain($product->qty) .'");';

Cheers

Joined: 08/07/2007
Juice: 15375

Hmm... I think that's what the drupal_to_js() function is for, but apparently we're not using somewhere. Which version of Ubercart did you find this in?

Joined: 09/06/2008
Juice: 9

Hi.

Are the note and Drupal version info on Track e-commerce statistics in Analytics with Ubercart outdated in light of the above and http://drupal.org/node/280090?

Joined: 08/14/2007
Juice: 3913

Think so, I don't think there's any issue with Google Analytics now. We use the latest GA code which has been in Ubercart core for a while now and are seeing all of our conversion info.

Help directly fund development: Donate via PayPal!