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

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

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>

AttachmentSize
uc_googleanalytics-1.1.tar.gz2.47 KB
--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.gz2.5 KB
--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 3898
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

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

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 47
Joined: 08/08/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

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

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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?)

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 3
Joined: 03/14/2008

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.

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 3
Joined: 03/14/2008

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

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 3898
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

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

Posts: 65
Joined: 01/18/2008
Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 30
Joined: 09/28/2007

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

Posts: 992
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

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.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 30
Joined: 09/28/2007

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

- txcrew