Universal Google Analytics, mini cart, and AutoMagiCache

jingarijingari Member
in General edited May 2014
Hi -
we're working on implementing Universal google analytics, but having trouble getting the ga code passed along via custom field to the cart page and checkout page. The way our store works is using a minicart dropdown, so when a customer adds something to the cart, the minicart shows and there they have the option to either view the cart page or go directly to the checkout page. This all seems to be working fine on our dev site, the ga code is added to customfields, but one thing I have noticed is that in our template caching for our production site, the javascript and css files are not being pulled out the way they are on the dev site, I am not sure if that's possibly part of the problem?

Any help would be much appreciated.
Thanks,
Jessica
Comments
  • jingarijingari Member
    edited May 2014
    It works inconsistently on the dev environment at the moment.
  • fc_adamfc_adam FoxyCart Team
    @jingari,

    In your uw.view-cart.js file, try not wrapping that code inside the document.ready call - I think that is causing that block to run after the later blocks within the page, which is overwriting them. They should be fine to run straight away rather than on document.ready.
  • jingarijingari Member
    The issue seems to be related to adding the custom field value for "ga". I moved the code from the page it self into the uw.view-cart.js (removed the document ready call as you mentioned) and changed it to this:
    fcc.events.cart.preprocess.add(function(e, arr) {
        ga(function(tracker) {
          jQuery.getJSON('https://' + storedomain + '/cart?' + fcc.session_get() + '&h:ga=' + escape(tracker.get('clientId')) + '&output=json&callback=?', function(data){
            fcc.events.cart.process.resume();
          });
        });
        return true;
      });
    

    Doing it this way, the ga custom field is being added, so the transaction can be tracked across the domain switch, but I am getting a type undefined error for the next section of
    if (e.tagName == 'A') {
    
  • fc_adamfc_adam FoxyCart Team
    @jingari,

    Yeah - moving it in there would have gotten it working, or I believe simply removing the document.ready call and leaving the code where it was would work as well. The issue was the block inside the document.ready call was resetting the process function event queue, so it was blowing away the ga event that was there previously.

    Looking at your page, if I add this using Chrome browser console, it works just fine:
    fcc.events.cart.process.add_pre(function(e){ console.log(e.tagName); });
    

    But for some reason, it's failing for your code there. Quite strange! Could you try logging what e is within that function you've written?
  • jingarijingari Member
    I've added that - I'm still getting e undefined when the initial Add to cart is run. I think it may have to do with the way I have set up the mini cart though. It looks like the GA tracking is all set now though.
  • fc_adamfc_adam FoxyCart Team
    @jingari,

    What does the console log for 'e' within your function?
  • jingarijingari Member
    it's FORM when the Add to cart button is pushed, but then it runs through again and gets the undefined error
  • fc_adamfc_adam FoxyCart Team
    @jingari,

    So that's the key issue then there - it shouldn't be running twice for a single add to cart action.

    Ah actually - I think I found the issue. You changed how/when the GA session is added into the JSON cart - so that needs a slight change, specifically this:
    fcc.events.cart.preprocess.add(function(e, arr) {
      //fcc.events.cart.process.add_pre(function(e, arr) {
        console.log("COUNT IS = "+count);
        //if(count < 1){
        console.log("E="+e.tagName);
      ga(function(tracker) {
        jQuery.getJSON('https://' + storedomain + '/cart?' + fcc.session_get() + '&h:ga=' + escape(tracker.get('clientId')) + '&output=json&callback=?', function(data){
          fcc.events.cart.process.resume();
        });
      });
      //}
      return true;
    });
    

    Would become:
    fcc.events.cart.preprocess.add(function(e, arr) {
      ga(function(tracker) {
        jQuery.getJSON('https://' + storedomain + '/cart?' + fcc.session_get() + '&h:ga=' + escape(tracker.get('clientId')) + '&output=json&callback=?', function(data){
          fcc.events.cart.preprocess.resume();
        });
      });
      return "pause";
    });
    
  • jingarijingari Member
    change from true to "pause" prevents the mini cart from opening. And if i change preprocess.add to process.add_pre, then the view cart and checkout buttons don't work.

    Where I have it now, there's still an error being popped, but it's functioning properly otherwise. I don't know why it's going back through that fcc.events.cart.process.add function a second time, but it is because the count is being increased, and when it does, that's when the error of undefined e is triggered.
  • fc_adamfc_adam FoxyCart Team
    @jingari,

    It's because you're resuming the process event queue from within the preprocess events. Did you copy the function as I said to update it above? While it did pause the preprocess events, it also restarted it from within the success function of the getJSON call.
  • jingarijingari Member
    Ah, I continued to miss-read the fcc.events.cart.preprocess.resume(); as fcc.events.cart.process.resume(); I think that fixed it. Running through tests now. Thank you much!
  • fc_adamfc_adam FoxyCart Team
    @jingari,

    Awesome! Glad that got it working! Let us know if you bump into anything else.
Sign In or Register to comment.