Gathering detailed insights and metrics for @eunjae-lee/search-insights-vite-test
Gathering detailed insights and metrics for @eunjae-lee/search-insights-vite-test
Gathering detailed insights and metrics for @eunjae-lee/search-insights-vite-test
Gathering detailed insights and metrics for @eunjae-lee/search-insights-vite-test
Library for reporting click, conversion and view metrics using the Algolia Insights API
npm install @eunjae-lee/search-insights-vite-test
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (96.08%)
JavaScript (3.58%)
Shell (0.34%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
59 Stars
447 Commits
19 Forks
54 Watchers
6 Branches
276 Contributors
Updated on May 21, 2025
Latest Version
0.0.7
Package Id
@eunjae-lee/search-insights-vite-test@0.0.7
Unpacked Size
63.31 kB
Size
14.23 kB
File Count
14
NPM Version
6.14.15
Node Version
14.17.6
Published on
Dec 17, 2021
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Search Insights lets you report click, conversion and view metrics using the Algolia Insights API.
You're looking at the documentation of search-insights
v2, which is the new major version. (Click here for v1.x documentation.)
v2 introduces a breaking change which is useCookie
being false
by default. If it's false
, search-insights
doesn't generate anonymous userToken. It means no event will be sent until setUserToken
is explicitly called.
Since v2.0.4, search-insights no longer validates event payloads. You can visit https://algolia.com/events/debugger instead.
Are you using Google Tag Manager in your app? We provide a custom template to ease the integration.
The Search Insights library can be either loaded via jsDelivr CDN or directly bundled with your application. We recommend loading the library by adding the snippet below to all pages where you wish to track search analytics.
1<script> 2var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@2.2.1"; 3 4!function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e[s]=e[s]||function(){ 5(e[s].queue=e[s].queue||[]).push(arguments)},i=a.createElement(t),c=a.getElementsByTagName(t)[0], 6i.async=1,i.src=n,c.parentNode.insertBefore(i,c) 7}(window,document,"script",ALGOLIA_INSIGHTS_SRC,"aa"); 8</script>
1aa('init', { 2 appId: 'APP_ID', 3 apiKey: 'SEARCH_API_KEY', 4}); 5 6// Optional: set the analytics user ID 7aa('setUserToken', 'USER_ID');
Option | Type | Default | Description |
---|---|---|---|
appId | string | None (required) | The identifier of your Algolia application |
apiKey | string | None (required) | The search API key of your Algolia application |
userHasOptedOut | boolean | false | Whether to exclude users from analytics |
region | 'de' | 'us' | Automatic | The DNS server to target |
useCookie | boolean | false | Whether to use cookie in browser environment. The anonymous user token will not be set if false . When useCookie is false and setUserToken is not called yet, sending events will throw errors because there is no user token to attach to the events. |
cookieDuration | number | 15552000000 (6 months) | The cookie duration in milliseconds |
userToken | string | undefined (optional) | Initial userToken. When given, anonymous userToken will not be set. |
When using Require.js, the default UMD build might conflict and throw with a "Mismatched anonymous define() modules" message. This is a known Require.js issue.
To work around this problem and ensure you capture all interactions occurring before the library is done loading, change ALGOLIA_INSIGHTS_SRC
to point to the IIFE build, and load it via a <script>
tag.
1<script> 2var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@2.0.2/dist/search-insights.browser.iife.js"; 3 4!function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e[s]=e[s]||function(){ 5(e[s].queue=e[s].queue||[]).push(arguments)},i=a.createElement(t),c=a.getElementsByTagName(t)[0], 6i.async=1,i.src=n,c.parentNode.insertBefore(i,c) 7}(window,document,"script",ALGOLIA_INSIGHTS_SRC,"aa"); 8</script>
(Node.js >= 8.16.0
required)
Insights library can be used on the backend as a Node.js module.
1npm install search-insights 2# or 3yarn add search-insights
1const aa = require('search-insights'); 2 3aa('init', { 4 appId: 'APPLICATION_ID', 5 apiKey: 'SEARCH_API_KEY' 6});
userToken
On the Node.js environment, unlike the browser environment, userToken
must be specified when sending any event.
1aa('clickedObjectIDs', { 2 userToken: 'USER_ID', 3 // ... 4});
If you want to customize the way to send events, you can create a custom Insights client.
1// via ESM 2import { createInsightsClient } from "search-insights"; 3// OR in commonJS 4const { createInsightsClient } = require("search-insights"); 5// OR via the UMD 6const createInsightsClient = window.AlgoliaAnalytics.createInsightsClient; 7 8function requestFn(url, data) { 9 const serializedData = JSON.stringify(data); 10 const { protocol, host, path } = require("url").parse(url); 11 const options = { 12 protocol, 13 host, 14 path, 15 method: "POST", 16 headers: { 17 "Content-Type": "application/json", 18 "Content-Length": serializedData.length 19 } 20 }; 21 22 const { request: nodeRequest } = 23 url.indexOf("https://") === 0 ? require("https") : require("http"); 24 const req = nodeRequest(options); 25 26 req.on("error", error => { 27 console.error(error); 28 }); 29 30 req.write(serializedData); 31 req.end(); 32}; 33 34const aa = createInsightsClient(requestFn);
The Search Insights library supports both Search and Personalization Algolia features.
To enable click analytics, the search parameter clickAnalytics
must be set to true
. This tells the Algolia engine to return a queryID
on each search request.
1const searchClient = algoliasearch('APPLICATION_ID', 'SEARCH_API_KEY'); 2const search = instantsearch({ 3 indexName: 'INDEX_NAME', 4 searchClient, 5 searchParameters: { 6 clickAnalytics: true, 7 }, 8}); 9 10function getQueryID() { 11 return search.helper.lastResults.queryID; 12}
1aa('clickedObjectIDsAfterSearch', { 2 index: 'INDEX_NAME', 3 eventName: 'Click item', 4 queryID: getQueryID(), 5 objectIDs: ['object1'], 6 positions: [42], 7});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
objectIDs | string[] | The list of IDs of the result that was clicked |
positions | number[] | The list of the absolute positions of the HTML element that was clicked (1-based and not 0-based) |
queryID | string | The queryID of the search sent from Algolia |
1aa('convertedObjectIDsAfterSearch', { 2 index: 'INDEX_NAME', 3 eventName: 'Add to basket', 4 queryID: getQueryID(), 5 objectIDs: ['object1'], 6});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
objectIDs | string[] | The list of IDs of the result that was clicked |
queryID | string | The queryID of the search sent from Algolia |
To enable personalization, the search parameter enablePersonalization
must be set to true
.
1const searchClient = algoliasearch('APPLICATION_ID', 'SEARCH_API_KEY'); 2const search = instantsearch({ 3 indexName: 'INDEX_NAME', 4 searchClient, 5 searchParameters: { 6 enablePersonalization: true, 7 }, 8});
userToken
In cases where the userToken
is generated, you need a way to access the userToken
so that you can pass it to the searchClient
.
1const searchClient = algoliasearch('APPLICATION_ID', 'SEARCH_API_KEY'); 2 3aa('getUserToken', null, (err, userToken) => { 4 // for algoliasearch v3.x 5 searchClient.setExtraHeader('X-Algolia-UserToken', userToken); 6 7 // for algoliasearch v4.x 8 searchClient.transporter.headers['X-Algolia-UserToken'] = userToken; 9});
userToken
changeIf you want to attach a listener for userToken
change, you can call onUserTokenChange
.
1aa('onUserTokenChange', (userToken) => { 2 console.log("userToken has changed: ", userToken); 3});
onUserTokenChange
accepts callback
(required) and options
(optional).
1aa('onUserTokenChange', callback, options);
Option | Type | Description |
---|---|---|
immediate | boolean | Fire the callback as soon as it's attached |
1aa('init', { ..., useCookie: true }); // ← This sets an anonymous user token if cookie is available. 2 3aa('onUserTokenChange', (userToken) => { 4 console.log(userToken); // prints out the anonymous user token 5}, { immediate: true });
1aa('init', { ... }); 2aa('setUserToken', 'my-user-id-1'); 3 4aa('onUserTokenChange', (userToken) => { 5 console.log(userToken); // prints out 'my-user-id-1' 6}, { immediate: true })
With immediate: true
, onUserTokenChange
will be immediately fired with the token which is set beforehand.
1aa('clickedObjectIDs', { 2 index: 'INDEX_NAME', 3 eventName: 'Add to basket', 4 objectIDs: ['object1'], 5});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
objectIDs | string[] | The list of IDs of the result that was clicked |
1aa('clickedFilters', { 2 index: 'INDEX_NAME', 3 eventName: 'Filter on facet', 4 filters: ['brand:Apple'], 5});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
filters | string[] | The list of filters that was clicked as '${attr}${op}${value}' |
1aa('convertedObjectIDs', { 2 index: 'INDEX_NAME', 3 eventName: 'Add to basket', 4 objectIDs: ['object1'], 5});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
objectIDs | string[] | The list of IDs of the result that was clicked |
1aa('convertedFilters', { 2 index: 'INDEX_NAME', 3 eventName: 'Filter on facet', 4 filters: ['brand:Apple'], 5});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
filters | string[] | The list of filters that was clicked as '${attr}${op}${value}' |
1aa('viewedObjectIDs', { 2 index: 'INDEX_NAME', 3 eventName: 'Add to basket', 4 objectIDs: ['object1'], 5});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
objectIDs | string[] | The list of IDs of the result that was clicked |
1aa('viewedFilters', { 2 index: 'INDEX_NAME', 3 eventName: 'Filter on facet', 4 filters: ['brand:Apple'], 5});
Option | Type | Description |
---|---|---|
index | string | The name of the index related to the event |
eventName | string | The name of the event |
filters | string[] | The list of filters that was clicked as '${attr}${op}${value}' |
The following examples assume that the Search Insights library is loaded.
To run the examples and the code, you need to run two separate commands:
yarn dev
runs webpack and the Node.js serveryarn build:dev
runs Rollup in watch modeTo release, go on master
(git checkout master
) and use:
1yarn run release
It will create a pull request for the next release. When it's reviewed, approved and merged, then CircleCI will automatically publish it to npm.
Search Insights is MIT licensed.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 25/26 approved changesets -- score normalized to 9
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
26 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More