Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS tracker should return promise #17756

Open
SassNinja opened this issue Jul 12, 2021 · 1 comment
Open

JS tracker should return promise #17756

SassNinja opened this issue Jul 12, 2021 · 1 comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.

Comments

@SassNinja
Copy link

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

  • Matomo Version: latest (4.3.1)
  • PHP Version: 7.4.20
  • Server Operating System:
  • Additionally installed plugins:
@SassNinja SassNinja added the Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. label Jul 12, 2021
@tsteur
Copy link
Member

tsteur commented Jul 12, 2021

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);
             };
 
             /**``` 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.
Projects
None yet
Development

No branches or pull requests

3 participants