Skip to content

Commit

Permalink
Merge pull request #648 from Shopify/add-tracking-events
Browse files Browse the repository at this point in the history
Add new tracking events
  • Loading branch information
melissaluu authored Sep 24, 2019
2 parents 15e0fd8 + 8a7d17b commit 530918b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export default class Cart extends Component {

onCheckout() {
this._userEvent('openCheckout');
this.props.tracker.track('Open cart checkout', {});
this.checkout.open(this.model.webUrl);
}

Expand Down
1 change: 1 addition & 0 deletions src/components/product-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default class ProductSet extends Component {
variantName: variant.title,
price: variant.priceV2.amount,
sku: null,
isProductSet: true,
});
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/components/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ export default class Product extends Component {
};
}

/**
* get info about product to be sent to tracker
* @return {Object}
*/
get productTrackingInfo() {
return {
id: this.model.id,
};
}

/**
* get configuration object for product details modal based on product config and modalProduct config.
* @return {Object} configuration object.
Expand Down Expand Up @@ -599,6 +609,7 @@ export default class Product extends Component {
}
} else if (this.options.buttonDestination === 'modal') {
this.props.setActiveEl(target);
this.props.tracker.track('Open modal', this.productTrackingInfo);
this.openModal();
} else if (this.options.buttonDestination === 'onlineStore') {
this.openOnlineStore();
Expand Down
40 changes: 38 additions & 2 deletions test/unit/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ let cart;
describe('Cart class', () => {
const moneyFormat = '${{amount}}';
let closeCartSpy;
let trackSpy;

beforeEach(() => {
closeCartSpy = sinon.spy();
trackSpy = sinon.spy();

cart = new Cart({
options: {
cart: {
Expand Down Expand Up @@ -44,12 +47,14 @@ describe('Cart class', () => {
trackMethod: (fn) => {
return function () {
fn(...arguments);
}
}
};
},
track: trackSpy,
},
closeCart: closeCartSpy,
});
});

afterEach(() => {
cart.destroy();
});
Expand Down Expand Up @@ -772,6 +777,37 @@ describe('Cart class', () => {
});
});

describe('onCheckout()', () => {
let openCheckoutStub;
let userEventStub;

beforeEach(() => {
openCheckoutStub = sinon.stub(cart.checkout, 'open');
userEventStub = sinon.stub(cart, '_userEvent');
cart.onCheckout();
});

afterEach(() => {
openCheckoutStub.restore();
userEventStub.restore();
});

it('triggers open checkout user event', () => {
assert.calledOnce(userEventStub);
assert.calledWith(userEventStub, 'openCheckout');
});

it('tracks open checkout', () => {
assert.calledOnce(trackSpy);
assert.calledWith(trackSpy, 'Open cart checkout', {});
});

it('open checkout', () => {
assert.calledOnce(openCheckoutStub);
assert.calledWith(openCheckoutStub, cart.model.webUrl);
});
});

describe('get cartNote', () => {
it('returns the note from the cart model', () => {
const note = 'test cart note';
Expand Down
1 change: 1 addition & 0 deletions test/unit/product-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ describe('ProductSet class', () => {
contents: expectedContentString,
checkoutPopup: set.config.cart.popup,
sku: null,
isProductSet: true,
}]);
});
});
Expand Down
19 changes: 19 additions & 0 deletions test/unit/product/product-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ describe('Product Component class', () => {
assert.calledWith(setActiveElSpy, target);
});

it('tracks open modal', () => {
assert.calledOnce(trackSpy);
assert.calledWith(trackSpy, 'Open modal', product.productTrackingInfo);
});

it('opens modal', () => {
assert.calledOnce(openModalStub);
});
Expand Down Expand Up @@ -2215,6 +2220,7 @@ describe('Product Component class', () => {

beforeEach(() => {
product.config.product.buttonDestination = 'cart';
product.model.id = 'lakjjk3ls3546lslsdkjf==';
product.model.variants = [
{
id: 'Xkdljlejkskskl3Zsike',
Expand Down Expand Up @@ -2295,6 +2301,19 @@ describe('Product Component class', () => {
});
});

describe('productTrackingInfo', () => {
beforeEach(() => {
product.model.id = 'Xkldjfjkej3l4jl3j5ljsodjflll';
});

it('returns a tracking info object with product id', () => {
const expectedObject = {
id: product.model.id,
};
assert.deepEqual(product.productTrackingInfo, expectedObject);
});
});

describe('onlineStore getters', () => {
let windowStub;
const expectedQs = '?channel=buy_button&referrer=http%3A%2F%2Ftest.com&variant=12345&';
Expand Down

0 comments on commit 530918b

Please sign in to comment.