ais-infinite-hits
You are reading the documentation for Angular InstantSearch v3, which is in beta. You can find the v2 documentation here.
<ais-infinite-hits // Optional parameters showPrevious="boolean" showPreviousLabel="string" showMoreLabel="string" [transformItems]="function" ></ais-infinite-hits>
About this widget
The ais-infinite-hits
is used to display a list of results. To configure the number of hits to show, use either:
- the
ais-hits-per-page
widget, - or the
ais-configure
widget and setting thehitsPerPage
insearchParameters
.
Examples
1
<ais-infinite-hits></ais-infinite-hits>
Properties
showPrevious
|
type: boolean
default: false
Optional
Enable the button to load previous results. The button is only displayed if the routing option is enabled in |
||
showPreviousLabel
|
type: string
default: "Show previous results"
Optional
Label used on the “Show previous” button. |
||
showMoreLabel
|
type: string
default: "Show more results"
Optional
Label used on the “Show more” button. |
||
transformItems
|
type: function
default: items => items
Optional
Receives the items, and is called before displaying them. Should return a new array with the same shape as the original array. Useful for mapping over the items to transform, and remove or reorder them. |
||
Copy
|
Templates
hits
|
type: object[]
The matched hits from the Algolia API. You can leverage the highlighting feature of Algolia with the |
||
results
|
type: object
The complete response from the Algolia API. It contains the |
||
isFirstPage
|
type: boolean
Indicates whether the first page of hits has been reached. |
||
isLastPage
|
type: boolean
Indicates whether the last page of hits has been reached. |
||
showPrevious
|
type: function
Loads the previous page of hits. |
||
Copy
|
|||
showMore
|
type: function
Loads the next page of hits. |
||
Copy
|
Sending Click and Conversion events
InstantSearch can connect with the Insights API client for JavaScript to allow sending click and conversion events right from the ais-infinite-hits
widget.
Requirements
This requires installing the search-insights library separately:
You can refer to our insights API documentation for more details.
It’s a 3-step process:
1. Install the Insights API client for JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
<script>
var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@1.1.1";
!function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e.aa=e.aa||function(){
(e.aa.queue=e.aa.queue||[]).push(arguments)},i=a.createElement(t),c=a.getElementsByTagName(t)[0],
i.async=1,i.src=ALGOLIA_INSIGHTS_SRC,c.parentNode.insertBefore(i,c)
}(window,document,"script",0,"aa");
aa('init', {
appId: 'YourApplicationID',
apiKey: 'YourSearchOnlyAPIKey',
});
</script>
2. Connect the Insights API client for JavaScript with Angular InstantSearch and enable clickAnalytics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import * as algoliasearch from 'algoliasearch/lite';
@Component({
template: `
<ais-instantsearch [config]="config">
<ais-configure [searchParameters]="{ clickAnalytics: true }"></ais-configure>
</ais-instantsearch>
`,
})
export class AppComponent {
config = {
indexName: 'instant_search',
searchClient: algoliasearch('YourApplicationID', 'YourAPIKey'),
insightsClient: (window as any).aa,
}
}
3. Call the insights function from your ais-infinite-hits
component
1
2
3
4
5
6
7
8
9
10
11
12
<ais-infinite-hits>
<ng-template let-hits="hits" let-insights="insights">
<div *ngFor="let hit of hits">
<a href="/product?queryID={{hit.__queryID}}">
<ais-highlight attribute="name" [hit]="hit"></ais-highlight>
</a>
<button (click)="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [hit.objectID] })">
Add to cart
</button>
</div>
</ng-template>
</ais-infinite-hits>
Provided props
insights
|
type: function
signature: (method: string, payload: object) => void
Sends insights events.
When not provided,
It’s worth noting that each element of
For more details on the contraints that apply to each |
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<ais-infinite-hits>
<ng-template let-hits="hits" let-insights="insights">
<div *ngFor="let hit of hits">
<a href="/product?queryID={{hit.__queryID}}">
<ais-highlight attribute="name" [hit]="hit"></ais-highlight>
</a>
<button (click)="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to favorite', objectIDs: [hit.objectID] })">
Add to favorite
</button>
<button (click)="insights('convertedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [hit.objectID] })">
Add to cart
</button>
</div>
</ng-template>
</ais-infinite-hits>
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div class="ais-InfiniteHits">
<button class="ais-InfiniteHits-showPrevious">Show previous results</button>
<ul class="ais-InfiniteHits-list">
<li class="ais-InfiniteHits-item">
...
</li>
<li class="ais-InfiniteHits-item">
...
</li>
<li class="ais-InfiniteHits-item">
...
</li>
</ul>
<button class="ais-InfiniteHits-showMore">Show more results</button>
</div>