Skip to content

Commit

Permalink
Reset the list of items in the cart, after tracking an ecommerce conv…
Browse files Browse the repository at this point in the history
…ersion

But do not reset the list of itms in the cart after tracking an ecommerce cart update.
fixes #10252
  • Loading branch information
mattab committed Jul 8, 2016
1 parent 9d598ce commit 590a9a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
12 changes: 10 additions & 2 deletions js/piwik.js
Expand Up @@ -3970,9 +3970,10 @@ if (typeof window.Piwik !== 'object') {
lastEcommerceOrderTs,
now = new Date(),
items = [],
sku;
sku,
isEcommerceOrder = String(orderId).length;

if (String(orderId).length) {
if (isEcommerceOrder) {
request += '&ec_id=' + encodeWrapper(orderId);
// Record date of order in the visitor cookie
lastEcommerceOrderTs = Math.round(now.getTime() / 1000);
Expand Down Expand Up @@ -4028,6 +4029,10 @@ if (typeof window.Piwik !== 'object') {
}
request = getRequest(request, configCustomData, 'ecommerce', lastEcommerceOrderTs);
sendRequest(request, configTrackerPause);

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

function logEcommerceOrder(orderId, grandTotal, subTotal, tax, shipping, discount) {
Expand Down Expand Up @@ -6320,6 +6325,7 @@ if (typeof window.Piwik !== 'object') {
* Adds an item (product) that is in the current Cart or in the Ecommerce order.
* This function is called for every item (product) in the Cart or the Order.
* The only required parameter is sku.
* The items are deleted from this JavaScript object when the Ecommerce order is tracked via the method trackEcommerceOrder.
*
* @param string sku (required) Item's SKU Code. This is the unique identifier for the product.
* @param string name (optional) Item's name
Expand All @@ -6338,6 +6344,7 @@ if (typeof window.Piwik !== 'object') {
* If the Ecommerce order contains items (products), you must call first the addEcommerceItem() for each item in the order.
* All revenues (grandTotal, subTotal, tax, shipping, discount) will be individually summed and reported in Piwik reports.
* Parameters orderId and grandTotal are required. For others, you can set to false if you don't need to specify them.
* After calling this method, items added to the cart will be deleted.
*
* @param string|int orderId (required) Unique Order ID.
* This will be used to count this order only once in the event the order page is reloaded several times.
Expand All @@ -6356,6 +6363,7 @@ if (typeof window.Piwik !== 'object') {
* Tracks a Cart Update (add item, remove item, update item).
* On every Cart update, you must call addEcommerceItem() for each item (product) in the cart, including the items that haven't been updated since the last cart update.
* Then you can call this function with the Cart grandTotal (typically the sum of all items' prices)
* Calling this method does not remove from this JavaScript object the items that were added to the cart via addEcommerceItem
*
* @param float grandTotal (required) Items (products) amount in the Cart
*/
Expand Down
2 changes: 1 addition & 1 deletion piwik.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions tests/javascript/index.php
Expand Up @@ -3033,7 +3033,7 @@ function getLastInteractionType() { return lastInteractionType; }
});

test("tracking", function() {
expect(117);
expect(118);

// Prevent Opera and HtmlUnit from performing the default action (i.e., load the href URL)
var stopEvent = function (evt) {
Expand Down Expand Up @@ -3287,8 +3287,12 @@ function getLastInteractionType() { return lastInteractionType; }
tracker3.addEcommerceItem("SKU NO PRICE NO QUANTITY", "PRODUCT NAME 3", "CATEGORY", "", "" );
tracker3.addEcommerceItem("SKU ONLY" );
tracker3.trackEcommerceCartUpdate( 555.55 );

tracker3.trackEcommerceOrder( "ORDER ID YES", 666.66, 333, 222, 111, 1 );

// the same order tracked once more, should have no items
tracker3.trackEcommerceOrder( "ORDER WITHOUT ANY ITEM", 777, 444, 222, 111, 1 );

// do not track
tracker3.setDoNotTrack(false);

Expand Down Expand Up @@ -3351,7 +3355,7 @@ function getLastInteractionType() { return lastInteractionType; }
xhr.open("GET", "piwik.php?requests=" + getToken(), false);
xhr.send(null);
results = xhr.responseText;
equal( (/<span\>([0-9]+)\<\/span\>/.exec(results))[1], "35", "count tracking events" );
equal( (/<span\>([0-9]+)\<\/span\>/.exec(results))[1], "36", "count tracking events" );

// firing callback
ok( trackLinkCallbackFired, "trackLink() callback fired" );
Expand Down Expand Up @@ -3428,6 +3432,9 @@ function getLastInteractionType() { return lastInteractionType; }
// Cart update
ok( /idgoal=0&revenue=555.55&ec_items=%5B%5B%22SKU%20PRODUCT%22%2C%22random%22%2C%22random%20PRODUCT%20CATEGORY%22%2C11.1111%2C2%5D%2C%5B%22SKU%20ONLY%20SKU%22%2C%22%22%2C%22%22%2C0%2C1%5D%2C%5B%22SKU%20ONLY%20NAME%22%2C%22PRODUCT%20NAME%202%22%2C%22%22%2C0%2C1%5D%2C%5B%22SKU%20NO%20PRICE%20NO%20QUANTITY%22%2C%22PRODUCT%20NAME%203%22%2C%22CATEGORY%22%2C0%2C1%5D%2C%5B%22SKU%20ONLY%22%2C%22%22%2C%22%22%2C0%2C1%5D%5D/.test( results ), "logEcommerceCartUpdate() with items" );

// Ecommerce order recorded twice, but each order empties the cart/list of items, so this order is empty of items
ok( /idgoal=0&ec_id=ORDER%20WITHOUT%20ANY%20ITEM&revenue=777&ec_st=444&ec_tx=222&ec_sh=111&ec_dt=1&ec_items=%5B%5D/.test( results ), "logEcommerceOrder() called twice, second time has no item" );

// parameters inserted by plugin hooks
ok( /testlog/.test( results ), "plugin hook log" );
ok( /testlink/.test( results ), "plugin hook link" );
Expand Down

0 comments on commit 590a9a2

Please sign in to comment.