0

I'm using a local version of analytics.js as a fallback for users that don't have an internet connection (but they can access this server in this case). It works fine, but then I do:

ga("require", "ecommerce");

And that seems to download it from www.google-analytics/plugins/wa/ecommerce.js anyway. Any way of changing this?

EDIT I am trying to proxy GA, and is using a local copy of analytics.js and a sendHitTask to redirect the answer to my own controller instead, and from there proxy the traffic to Google analytics servers. The server has internet connection, the users don't. You can read more about my situation here: Google Analytics proxy

Community
  • 1
  • 1
Tony Gustafsson
  • 828
  • 1
  • 7
  • 18
  • Write your own GA plugin that loads a local copy of ec.js ?(Documentation on plugins is here: https://developers.google.com/analytics/devguides/collection/analyticsjs/plugins#require) – Eike Pierstorff Mar 27 '15 at 09:14
  • You should provide links/context with your previous questions, else you provoke answers like Juliens who is not aware of your GA proxy. – Eike Pierstorff Mar 27 '15 at 11:05

1 Answers1

0

If you want to host your GA libraries locally, that's your own problem :-)

If you do go that route, just make sure you understand that ec.js and ecommerce.js are two different libraries (ec.js is for enhanced commerce only)

You could indeed use a setup similar to this:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.yoursite.local/analytics.js','ga');

  ga('create', 'UA-12345678-1', 'auto');

  (function(){var f=window,g="push",k="length",m="prototype",n={action:"pa",promoAction:"promoa",id:"ti",affiliation:"ta",revenue:"tr",tax:"tt",shipping:"ts",coupon:"tcc",step:"cos",label:"col",option:"col",options:"col",list:"pal",listSource:"pls"},p={id:"id",name:"nm",brand:"br",category:"ca",variant:"va",position:"ps",price:"pr",quantity:"qt",coupon:"cc","dimension(\\d+)":"cd","metric(\\d+)":"cm"},q={id:"id",name:"nm",creative:"cr",position:"ps"},r=function(a,d){this.name=a;this.source=d;this.e=[]},t=function(a){if(a.get&&
    a.set){this.clear();var d=a.get("buildHitTask");a.set("buildHitTask",s(this,d))}};t[m].clear=function(){this.b=void 0;this.c=[];this.a=[];this.d=[]};t[m].i=function(a,d){var c=d||{};"promo_click"==a?c.promoAction="click":c.action=a;this.b=u(c)};t[m].g=function(a){(a=u(a))&&this.c[g](a)};t[m].f=function(a){var d=u(a);if(d){var c,b=a.list||"";a=a.listSource||"";for(var e=0;e<this.a[k];e++)if(this.a[e].name==b){c=this.a[e];break}c||(c=new r(b,a),this.a[g](c));c.e[g](d)}};
    t[m].h=function(a){(a=u(a))&&this.d[g](a)};var s=function(a,d){return function(c){var b,e,h;a.b&&v(n,a.b,c,"&");for(b=0;b<a.c[k];b++)e="&pr"+(b+1),v(p,a.c[b],c,e);for(b=0;b<a.a[k];b++){h=a.a[b];e="&il"+(b+1);h.name&&c.set(e+"nm",h.name,!0);h.source&&c.set(e+"ls",h.source,!0);for(var l=0;l<h.e[k];l++)v(p,h.e[l],c,e+"pi"+(l+1))}for(b=0;b<a.d[k];b++)e="&promo"+(b+1),v(q,a.d[b],c,e);a.clear();return d(c)}};
    function u(a){var d=0,c={};if(a&&"object"==typeof a)for(var b in a)a.hasOwnProperty(b)&&(c[b]=a[b],d++);return d?c:void 0}function v(a,d,c,b){for(var e in d)if(d.hasOwnProperty(e))for(var h in a)if(a.hasOwnProperty(h)){var l=e.match("^"+h+"$");l&&c.set(b+a[h]+l.slice(1).join(""),d[e],!0)}}function w(a,d){t[m][a]=function(){return d.apply(this,arguments)}}
    (function(){f.gaplugins=f.gaplugins||{};f.gaplugins.EC=t;w("setAction",t[m].i);w("addProduct",t[m].g);w("addImpression",t[m].f);w("addPromo",t[m].h);w("clear",t[m].clear);var a=f.GoogleAnalyticsObject||"ga";f[a]=f[a]||function(){(f[a].q=f[a].q||[])[g](arguments)};f[a]("provide","ec",t)})();})();

  ga('send', 'pageview');

  /*
    transaction details go here
  */

</script>

Now remember that if your users don't have an internet connection, traffic and transactions won't get recorded unless you host data collection as well.

Julien Coquet
  • 217
  • 2
  • 6
  • 1
    Since I answered once of his previous questions I can say that Tony is aware of that - he actually proxies the GA requests through his own server using the measurement protocol. The thing is he still wants to use GAs own libraries instead of assembling the requests on his own. – Eike Pierstorff Mar 27 '15 at 11:03
  • ah i see, if Tony emulates the /collect call too, that would work. – Julien Coquet Mar 27 '15 at 11:11
  • Yes I should have mentioned that. I will provide some more info :) – Tony Gustafsson Mar 27 '15 at 12:03
  • However, you DO have a point... I could include the contents of ecommerce.js directly... but analytics.js is loaded async, so I think that might be a problem if ecommerce.js is loaded before analytics.js? – Tony Gustafsson Mar 27 '15 at 12:08