Skip to content

Commit

Permalink
Merge pull request #2115 from alphagov/msw/parts-in-results
Browse files Browse the repository at this point in the history
Allow using data-ecommerce-index to override implicit index
  • Loading branch information
barrucadu authored Mar 30, 2020
2 parents ecda4ed + dfddb1b commit 211ce40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/analytics/ecommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
var contentId = $ecommerceRow.attr('data-ecommerce-content-id'),
path = $ecommerceRow.attr('data-ecommerce-path');

var index_override = $ecommerceRow.attr('data-ecommerce-index');
index = index_override ? parseInt(index_override, 10) - 1 : index;

addImpression(contentId, path, index + startPosition, searchQuery, listTitle, listSubheading, variant);
trackProductOnClick($ecommerceRow, contentId, path, index + startPosition, searchQuery, listTitle, listSubheading, variant, trackClickLabel);
});
Expand Down
23 changes: 23 additions & 0 deletions spec/javascripts/analytics/ecommerce.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ describe('Ecommerce reporter for results pages', function() {
});
});

it('uses data-ecommerce-index to override the implicit index', function() {
element = $('\
<div data-ecommerce-start-index="1" data-search-query="search query">\
<div \
data-ecommerce-row\
data-ecommerce-path="/path/to/page"\
data-ecommerce-index="42"\
data-ecommerce-content-id="AAAA-1111">\
</div>\
</div>\
');

ecommerce.init(element);

expect(ga).toHaveBeenCalledWith('ec:addImpression', {
id: 'AAAA-1111',
name: "/path/to/page",
position: 42,
list: 'Site search results',
dimension71: 'search query'
});
});

it('tracks impressions with product variants', function() {
element = $('\
<div data-ecommerce-start-index="1" data-search-query="search query" data-ecommerce-variant="variant-x">\
Expand Down

0 comments on commit 211ce40

Please sign in to comment.