xsanisty/calx.js

All versions of calx.js seem to be breaking Google Analytics 4 and causing all the javascript to fail.

martindefatte opened this issue · 2 comments

All versions of calx.js seem to be breaking Google Analytics 4 and causing all the javascript to fail.

I've tried using v 1.1.9, 2.0.0 and 2.2.6 and they all seem to cause errors with the new Google Analytics 4. When the legacy analytics becomes deprecated in June of 2023, I'll have to make the decision to not collect analytics or not use the calc plugin. They both seem valuable in different ways.

I ran into the same problem and my solution is below. Hope this helps.

  • https://github.com/xsanisty/calx.js/blob/main/jquery-calx-2.2.8.js

  • Line 37 has an issue: if (typeof Array.indexOf !== "function") {

  • The left side of the argument returns undefined. At least it returns undefined at the point where the file was loaded in my pages. Executing this typeof Array.indexOf from the dev tools browser console does return "function" so my assumption is the file was loading before the brower's native indexOf function was there. The plugin is setting the prototype.indexOf for browsers that do not need it which causes the error in gtag.js

  • Change Line 37 to if (!Array.prototype.indexOf) {

  • All done. Now the plugin only sets prototype.indexOf for browsers that actually need it. Which aren't very many and old ones like IE8 that nobody should be concerned about. I'm pretty sure gtag.js won't work in browsers that would access the code in this if block anyway.

Thank you so much! This did the trick. I actually did it on the v1.x version of the plugin as well - been using this before 2.0 came along and I haven't converted the particular site over. MUCH APPRECIATED