matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.52k stars 2.61k forks source link

JS tracker should return promise #17756

Open SassNinja opened 3 years ago

SassNinja commented 3 years ago

I'm using matomo for a SPA and (currently) track page views and ecommerce actions. Regarding the latter I've a problem with tracking orders because after having finished an order I call trackEcommerceOrder AND and the same time redirect the user to another page (outside of my control)

As a result the matomo request is often not finished because of that redirect. I'd need a way to wait for matomo until the order tracking is done. After taking a look at the code base I think the push method (or something else) should return a promise instead of nothing. https://github.com/matomo-org/matomo/blob/4.x-dev/js/piwik.js#L4778

Can you confirm this is a missing feature or am I missing another possible way?

Summary

The push method should return a promise which gets resolved once the tracking is done to be able to wait until matomo is done.

Your Environment

tsteur commented 3 years ago

FYI some tracking methods support a callback as a parameter (not a problem). but seems the tracker ecommerce order method doesn't support this just yet. Haven't tested it but some change like below might make this work just in case you're keen on testing this and creating a PR if that works. Supporting promises instead of callbacks be great to eventually (but be a different issue probably)


diff --git a/js/piwik.js b/js/piwik.js
index ec25d4b86a..7a3a3be30f 100644
--- a/js/piwik.js
+++ b/js/piwik.js
@@ -3813,7 +3813,7 @@ if (typeof window.Matomo !== 'object') {
                 return false;
             };

-            function logEcommerce(orderId, grandTotal, subTotal, tax, shipping, discount) {
+            function logEcommerce(orderId, grandTotal, subTotal, tax, shipping, discount, callback) {
                 var request = 'idgoal=0',
                     now = new Date(),
                     items = [],
@@ -3873,23 +3873,23 @@ if (typeof window.Matomo !== 'object') {
                     request += '&ec_items=' + encodeWrapper(windowAlias.JSON.stringify(items));
                 }
                 request = getRequest(request, configCustomData, 'ecommerce');
-                sendRequest(request, configTrackerPause);
+                sendRequest(request, configTrackerPause, callback);

                 if (isEcommerceOrder) {
                     ecommerceItems = {};
                 }
             }

-            function logEcommerceOrder(orderId, grandTotal, subTotal, tax, shipping, discount) {
+            function logEcommerceOrder(orderId, grandTotal, subTotal, tax, shipping, discount, callback) {
                 if (String(orderId).length
                     && isDefined(grandTotal)) {
-                    logEcommerce(orderId, grandTotal, subTotal, tax, shipping, discount);
+                    logEcommerce(orderId, grandTotal, subTotal, tax, shipping, discount, callback);
                 }
             }

-            function logEcommerceCartUpdate(grandTotal) {
+            function logEcommerceCartUpdate(grandTotal, callback) {
                 if (isDefined(grandTotal)) {
-                    logEcommerce("", grandTotal, "", "", "", "");
+                    logEcommerce("", grandTotal, "", "", "", "", callback);
                 }
             }

@@ -5694,7 +5694,7 @@ if (typeof window.Matomo !== 'object') {
             this.setCookieDomain = function (domain) {
                 var domainFixed = domainFixup(domain);

-                if (isPossibleToSetCookieOnDomain(domainFixed)) {
+                if (configCookiesDisabled || isPossibleToSetCookieOnDomain(domainFixed)) {
                     configCookieDomain = domainFixed;
                     updateDomainHash();
                 }
@@ -6647,8 +6647,8 @@ if (typeof window.Matomo !== 'object') {
              * @param float shipping (optional) Shipping amount for this order
              * @param float discount (optional) Discounted amount in this order
              */
-            this.trackEcommerceOrder = function (orderId, grandTotal, subTotal, tax, shipping, discount) {
-                logEcommerceOrder(orderId, grandTotal, subTotal, tax, shipping, discount);
+            this.trackEcommerceOrder = function (orderId, grandTotal, subTotal, tax, shipping, discount, callback) {
+                logEcommerceOrder(orderId, grandTotal, subTotal, tax, shipping, discount, callback);
             };

             /**
@@ -6659,8 +6659,8 @@ if (typeof window.Matomo !== 'object') {
              *
              * @param float grandTotal (required) Items (products) amount in the Cart
              */
-            this.trackEcommerceCartUpdate = function (grandTotal) {
-                logEcommerceCartUpdate(grandTotal);
+            this.trackEcommerceCartUpdate = function (grandTotal, callback) {
+                logEcommerceCartUpdate(grandTotal, callback);
             };

             /**```