Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(insights): add insights support to InfiniteHits widget
Browse files Browse the repository at this point in the history
**Summary**
This integrates insights with Vue Instantsearch to help using Click
Analytics in InfiniteHits.

Using `connectInfiniteHitsWithInsights` instead of `connectInfiniteHits`
exposed a new property `insights` to the internal state, which user can
call to emit insights events.

**Result**
```html
<ais-infinite-hits>
  <div slot-scope="{ items, refine, insights }">

    <div v-for="item in items">
      <button @click="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [item.objectID] })">Add to cart</button>
    </div>

    <button @click="refine()"> Load more </button>
  </div>
</ais-infinite-hits>
```
  • Loading branch information
tkrugg committed Apr 24, 2019
1 parent 3d2eec9 commit e91bb90
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/components/InfiniteHits.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:refine-previous="refinePrevious"
:refine-next="refineNext"
:refine="refineNext"
:insights="state.insights"
>
<ol :class="suit('list')">
<li
Expand Down Expand Up @@ -59,13 +60,13 @@

<script>
import { createWidgetMixin } from '../mixins/widget';
import { connectInfiniteHits } from 'instantsearch.js/es/connectors';
import { connectInfiniteHitsWithInsights } from 'instantsearch.js/es/connectors';
import { createSuitMixin } from '../mixins/suit';
export default {
name: 'AisInfiniteHits',
mixins: [
createWidgetMixin({ connector: connectInfiniteHits }),
createWidgetMixin({ connector: connectInfiniteHitsWithInsights }),
createSuitMixin({ name: 'InfiniteHits' }),
],
props: {
Expand Down
28 changes: 28 additions & 0 deletions src/components/__tests__/InfiniteHits.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,31 @@ it('expect to call showMore on click', () => {

expect(showMore).toHaveBeenCalled();
});

it('exposes insights prop to the default slot', () => {
const insights = jest.fn();

__setState({
...defaultState,
insights,
});

const wrapper = mount(InfiniteHits, {
scopedSlots: {
default: `
<ul slot-scope="{ items, insights }">
<li v-for="(item, itemIndex) in items" >
<button :id="'add-to-cart-' + item.objectID" @click="insights('clickedObjectIDsAfterSearch', {eventName: 'Add to cart', objectIDs: [item.objectID]})">
Add to cart
</button>
</li>
</ul>
`,
},
});
wrapper.find('#add-to-cart-00002').trigger('click');
expect(insights).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
eventName: 'Add to cart',
objectIDs: ['00002'],
});
});
35 changes: 34 additions & 1 deletion stories/InfiniteHits.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { storiesOf } from '@storybook/vue';
import { action } from '@storybook/addon-actions';
import { simple } from 'instantsearch.js/es/lib/stateMappings';
import { previewWrapper } from './utils';
import { MemoryRouter } from './MemoryRouter';
import { simple } from 'instantsearch.js/es/lib/stateMappings';

storiesOf('ais-infinite-hits', module)
.addDecorator(previewWrapper())
Expand Down Expand Up @@ -124,3 +125,35 @@ storiesOf('ais-infinite-hits', module)
</div>
</ais-infinite-hits>`,
}));

storiesOf('ais-infinite-hits', module)
.addDecorator(
previewWrapper({
insightsClient: (method, payload) =>
action(`[InsightsClient] sent ${method} with payload`)(payload),
}),
)
.add('with insights', () => ({
template: `
<div>
<ais-configure :clickAnalytics="true" />
<ais-infinite-hits>
<div slot-scope="{ items, refine, insights }">
<div
v-for="item in items"
:key="item.objectID"
>
custom objectID: {{item.objectID}}
<button @click="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [item.objectID] })">Add to cart</button>
</div>
<button
:disabled="isLastPage"
@click="refine"
>
Load more
</button>
</div>
</ais-infinite-hits>
</div>
`,
}));

0 comments on commit e91bb90

Please sign in to comment.