This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
# @algolia/autocomplete-plugin-algolia-insights
The Algolia Insights plugin automatically sends click and conversion events to the [Algolia Insights API](https://www.algolia.com/doc/rest-api/insights]) whenever a user interacts with the autocomplete.
## Installation
```sh
yarn add @algolia/autocomplete-plugin-algolia-insights
# or
npm install @algolia/autocomplete-plugin-algolia-insights
```
## Documentation
See [**Documentation**](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights).

View File

@@ -0,0 +1,37 @@
import { AutocompletePlugin } from '@algolia/autocomplete-shared';
import { InsightsClient, OnActiveParams, OnItemsChangeParams, OnSelectParams } from './types';
export declare type CreateAlgoliaInsightsPluginParams = {
/**
* The initialized Search Insights client.
*
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/createAlgoliaInsightsPlugin/#param-insightsclient
*/
insightsClient?: InsightsClient;
/**
* Hook to send an Insights event when the items change.
*
* By default, it sends a `viewedObjectIDs` event.
*
* In as-you-type experiences, items change as the user types. This hook is debounced every 400ms to reflect actual items that users notice and avoid generating too many events for items matching "in progress" queries.
*
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/createAlgoliaInsightsPlugin/#param-onitemschange
*/
onItemsChange?(params: OnItemsChangeParams): void;
/**
* Hook to send an Insights event when an item is selected.
*
* By default, it sends a clickedObjectIDsAfterSearch event.
*
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/createAlgoliaInsightsPlugin/#param-onselect
*/
onSelect?(params: OnSelectParams): void;
/**
* Hook to send an Insights event when an item is active.
*
* By default, it doesn't send any events.
*
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/createAlgoliaInsightsPlugin/#param-onactive
*/
onActive?(params: OnActiveParams): void;
};
export declare function createAlgoliaInsightsPlugin(options: CreateAlgoliaInsightsPluginParams): AutocompletePlugin<any, undefined>;

View File

@@ -0,0 +1,200 @@
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { createRef, debounce, isEqual, noop, safelyRunOnBrowser } from '@algolia/autocomplete-shared';
import { createClickedEvent } from './createClickedEvent';
import { createSearchInsightsApi } from './createSearchInsightsApi';
import { createViewedEvents } from './createViewedEvents';
import { isAlgoliaInsightsHit } from './isAlgoliaInsightsHit';
var VIEW_EVENT_DELAY = 400;
var ALGOLIA_INSIGHTS_VERSION = '2.6.0';
var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@".concat(ALGOLIA_INSIGHTS_VERSION, "/dist/search-insights.min.js");
var sendViewedObjectIDs = debounce(function (_ref) {
var onItemsChange = _ref.onItemsChange,
items = _ref.items,
insights = _ref.insights,
state = _ref.state;
onItemsChange({
insights: insights,
insightsEvents: createViewedEvents({
items: items
}).map(function (event) {
return _objectSpread({
eventName: 'Items Viewed'
}, event);
}),
state: state
});
}, VIEW_EVENT_DELAY);
export function createAlgoliaInsightsPlugin(options) {
var _getOptions = getOptions(options),
providedInsightsClient = _getOptions.insightsClient,
onItemsChange = _getOptions.onItemsChange,
onSelectEvent = _getOptions.onSelect,
onActiveEvent = _getOptions.onActive;
var insightsClient = providedInsightsClient;
if (!providedInsightsClient) {
safelyRunOnBrowser(function (_ref2) {
var window = _ref2.window;
var pointer = window.AlgoliaAnalyticsObject || 'aa';
if (typeof pointer === 'string') {
insightsClient = window[pointer];
}
if (!insightsClient) {
window.AlgoliaAnalyticsObject = pointer;
if (!window[pointer]) {
window[pointer] = function () {
if (!window[pointer].queue) {
window[pointer].queue = [];
}
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
window[pointer].queue.push(args);
};
}
window[pointer].version = ALGOLIA_INSIGHTS_VERSION;
insightsClient = window[pointer];
loadInsights(window);
}
});
}
var insights = createSearchInsightsApi(insightsClient);
var previousItems = createRef([]);
var debouncedOnStateChange = debounce(function (_ref3) {
var state = _ref3.state;
if (!state.isOpen) {
return;
}
var items = state.collections.reduce(function (acc, current) {
return [].concat(_toConsumableArray(acc), _toConsumableArray(current.items));
}, []).filter(isAlgoliaInsightsHit);
if (!isEqual(previousItems.current.map(function (x) {
return x.objectID;
}), items.map(function (x) {
return x.objectID;
}))) {
previousItems.current = items;
if (items.length > 0) {
sendViewedObjectIDs({
onItemsChange: onItemsChange,
items: items,
insights: insights,
state: state
});
}
}
}, 0);
return {
name: 'aa.algoliaInsightsPlugin',
subscribe: function subscribe(_ref4) {
var setContext = _ref4.setContext,
onSelect = _ref4.onSelect,
onActive = _ref4.onActive;
insightsClient('addAlgoliaAgent', 'insights-plugin');
setContext({
algoliaInsightsPlugin: {
__algoliaSearchParameters: {
clickAnalytics: true
},
insights: insights
}
});
onSelect(function (_ref5) {
var item = _ref5.item,
state = _ref5.state,
event = _ref5.event;
if (!isAlgoliaInsightsHit(item)) {
return;
}
onSelectEvent({
state: state,
event: event,
insights: insights,
item: item,
insightsEvents: [_objectSpread({
eventName: 'Item Selected'
}, createClickedEvent({
item: item,
items: previousItems.current
}))]
});
});
onActive(function (_ref6) {
var item = _ref6.item,
state = _ref6.state,
event = _ref6.event;
if (!isAlgoliaInsightsHit(item)) {
return;
}
onActiveEvent({
state: state,
event: event,
insights: insights,
item: item,
insightsEvents: [_objectSpread({
eventName: 'Item Active'
}, createClickedEvent({
item: item,
items: previousItems.current
}))]
});
});
},
onStateChange: function onStateChange(_ref7) {
var state = _ref7.state;
debouncedOnStateChange({
state: state
});
},
__autocomplete_pluginOptions: options
};
}
function getOptions(options) {
return _objectSpread({
onItemsChange: function onItemsChange(_ref8) {
var insights = _ref8.insights,
insightsEvents = _ref8.insightsEvents;
insights.viewedObjectIDs.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
return _objectSpread(_objectSpread({}, event), {}, {
algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
});
})));
},
onSelect: function onSelect(_ref9) {
var insights = _ref9.insights,
insightsEvents = _ref9.insightsEvents;
insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
return _objectSpread(_objectSpread({}, event), {}, {
algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
});
})));
},
onActive: noop
}, options);
}
function loadInsights(environment) {
var errorMessage = "[Autocomplete]: Could not load search-insights.js. Please load it manually following https://alg.li/insights-autocomplete";
try {
var script = environment.document.createElement('script');
script.async = true;
script.src = ALGOLIA_INSIGHTS_SRC;
script.onerror = function () {
// eslint-disable-next-line no-console
console.error(errorMessage);
};
document.body.appendChild(script);
} catch (cause) {
// eslint-disable-next-line no-console
console.error(errorMessage);
}
}

View File

@@ -0,0 +1,9 @@
import type { AlgoliaInsightsHit, ClickedObjectIDsAfterSearchParams, InsightsParamsWithItems } from './types';
declare type CreateClickedEventParams = {
item: AlgoliaInsightsHit;
items: AlgoliaInsightsHit[];
};
export declare function createClickedEvent({ item, items, }: CreateClickedEventParams): Omit<InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>, 'eventName'> & {
algoliaSource?: string[];
};
export {};

View File

@@ -0,0 +1,13 @@
export function createClickedEvent(_ref) {
var item = _ref.item,
items = _ref.items;
return {
index: item.__autocomplete_indexName,
items: [item],
positions: [1 + items.findIndex(function (x) {
return x.objectID === item.objectID;
})],
queryID: item.__autocomplete_queryID,
algoliaSource: ['autocomplete']
};
}

View File

@@ -0,0 +1,59 @@
import { ClickedFiltersParams, ClickedObjectIDsAfterSearchParams, ClickedObjectIDsParams, ConvertedFiltersParams, ConvertedObjectIDsAfterSearchParams, ConvertedObjectIDsParams, InsightsClient, InsightsParamsWithItems, ViewedFiltersParams, ViewedObjectIDsParams } from './types';
export declare function createSearchInsightsApi(searchInsights: InsightsClient): {
/**
* Initializes Insights with Algolia credentials.
*/
init(appId: string, apiKey: string): void;
/**
* Sets the user token to attach to events.
*/
setUserToken(userToken: string): void;
/**
* Sends click events to capture a query and its clicked items and positions.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids-after-search/
*/
clickedObjectIDsAfterSearch(...params: Array<InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>>): void;
/**
* Sends click events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids/
*/
clickedObjectIDs(...params: Array<InsightsParamsWithItems<ClickedObjectIDsParams>>): void;
/**
* Sends click events to capture the filters a user clicks on.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-filters/
*/
clickedFilters(...params: ClickedFiltersParams[]): void;
/**
* Sends conversion events to capture a query and its clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids-after-search/
*/
convertedObjectIDsAfterSearch(...params: Array<InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>>): void;
/**
* Sends conversion events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids/
*/
convertedObjectIDs(...params: Array<InsightsParamsWithItems<ConvertedObjectIDsParams>>): void;
/**
* Sends conversion events to capture the filters a user uses when converting.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-filters/
*/
convertedFilters(...params: ConvertedFiltersParams[]): void;
/**
* Sends view events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-object-ids/
*/
viewedObjectIDs(...params: Array<InsightsParamsWithItems<ViewedObjectIDsParams>>): void;
/**
* Sends view events to capture the filters a user uses when viewing.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-filters/
*/
viewedFilters(...params: ViewedFiltersParams[]): void;
};

View File

@@ -0,0 +1,197 @@
var _excluded = ["items"],
_excluded2 = ["items"];
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { isModernInsightsClient } from './isModernInsightsClient';
function chunk(item) {
var chunkSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 20;
var chunks = [];
for (var i = 0; i < item.objectIDs.length; i += chunkSize) {
chunks.push(_objectSpread(_objectSpread({}, item), {}, {
objectIDs: item.objectIDs.slice(i, i + chunkSize)
}));
}
return chunks;
}
function mapToInsightsParamsApi(params) {
return params.map(function (_ref) {
var items = _ref.items,
param = _objectWithoutProperties(_ref, _excluded);
return _objectSpread(_objectSpread({}, param), {}, {
objectIDs: (items === null || items === void 0 ? void 0 : items.map(function (_ref2) {
var objectID = _ref2.objectID;
return objectID;
})) || param.objectIDs
});
});
}
export function createSearchInsightsApi(searchInsights) {
var canSendHeaders = isModernInsightsClient(searchInsights);
function sendToInsights(method, payloads, items) {
if (canSendHeaders && typeof items !== 'undefined') {
var _items$0$__autocomple = items[0].__autocomplete_algoliaCredentials,
appId = _items$0$__autocomple.appId,
apiKey = _items$0$__autocomple.apiKey;
var headers = {
'X-Algolia-Application-Id': appId,
'X-Algolia-API-Key': apiKey
};
searchInsights.apply(void 0, [method].concat(_toConsumableArray(payloads), [{
headers: headers
}]));
} else {
searchInsights.apply(void 0, [method].concat(_toConsumableArray(payloads)));
}
}
return {
/**
* Initializes Insights with Algolia credentials.
*/
init: function init(appId, apiKey) {
searchInsights('init', {
appId: appId,
apiKey: apiKey
});
},
/**
* Sets the user token to attach to events.
*/
setUserToken: function setUserToken(userToken) {
searchInsights('setUserToken', userToken);
},
/**
* Sends click events to capture a query and its clicked items and positions.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids-after-search/
*/
clickedObjectIDsAfterSearch: function clickedObjectIDsAfterSearch() {
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
params[_key] = arguments[_key];
}
if (params.length > 0) {
sendToInsights('clickedObjectIDsAfterSearch', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends click events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids/
*/
clickedObjectIDs: function clickedObjectIDs() {
for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
params[_key2] = arguments[_key2];
}
if (params.length > 0) {
sendToInsights('clickedObjectIDs', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends click events to capture the filters a user clicks on.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-filters/
*/
clickedFilters: function clickedFilters() {
for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
params[_key3] = arguments[_key3];
}
if (params.length > 0) {
searchInsights.apply(void 0, ['clickedFilters'].concat(params));
}
},
/**
* Sends conversion events to capture a query and its clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids-after-search/
*/
convertedObjectIDsAfterSearch: function convertedObjectIDsAfterSearch() {
for (var _len4 = arguments.length, params = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
params[_key4] = arguments[_key4];
}
if (params.length > 0) {
sendToInsights('convertedObjectIDsAfterSearch', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends conversion events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids/
*/
convertedObjectIDs: function convertedObjectIDs() {
for (var _len5 = arguments.length, params = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
params[_key5] = arguments[_key5];
}
if (params.length > 0) {
sendToInsights('convertedObjectIDs', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends conversion events to capture the filters a user uses when converting.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-filters/
*/
convertedFilters: function convertedFilters() {
for (var _len6 = arguments.length, params = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
params[_key6] = arguments[_key6];
}
if (params.length > 0) {
searchInsights.apply(void 0, ['convertedFilters'].concat(params));
}
},
/**
* Sends view events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-object-ids/
*/
viewedObjectIDs: function viewedObjectIDs() {
for (var _len7 = arguments.length, params = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
params[_key7] = arguments[_key7];
}
if (params.length > 0) {
params.reduce(function (acc, _ref3) {
var items = _ref3.items,
param = _objectWithoutProperties(_ref3, _excluded2);
return [].concat(_toConsumableArray(acc), _toConsumableArray(chunk(_objectSpread(_objectSpread({}, param), {}, {
objectIDs: (items === null || items === void 0 ? void 0 : items.map(function (_ref4) {
var objectID = _ref4.objectID;
return objectID;
})) || param.objectIDs
})).map(function (payload) {
return {
items: items,
payload: payload
};
})));
}, []).forEach(function (_ref5) {
var items = _ref5.items,
payload = _ref5.payload;
return sendToInsights('viewedObjectIDs', [payload], items);
});
}
},
/**
* Sends view events to capture the filters a user uses when viewing.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-filters/
*/
viewedFilters: function viewedFilters() {
for (var _len8 = arguments.length, params = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
params[_key8] = arguments[_key8];
}
if (params.length > 0) {
searchInsights.apply(void 0, ['viewedFilters'].concat(params));
}
}
};
}

View File

@@ -0,0 +1,6 @@
import { AlgoliaInsightsHit, InsightsParamsWithItems, ViewedObjectIDsParams } from './types';
declare type CreateViewedEventsParams = {
items: AlgoliaInsightsHit[];
};
export declare function createViewedEvents({ items, }: CreateViewedEventsParams): Array<Omit<InsightsParamsWithItems<ViewedObjectIDsParams>, 'eventName'>>;
export {};

View File

@@ -0,0 +1,16 @@
export function createViewedEvents(_ref) {
var items = _ref.items;
var itemsByIndexName = items.reduce(function (acc, current) {
var _acc$current$__autoco;
acc[current.__autocomplete_indexName] = ((_acc$current$__autoco = acc[current.__autocomplete_indexName]) !== null && _acc$current$__autoco !== void 0 ? _acc$current$__autoco : []).concat(current);
return acc;
}, {});
return Object.keys(itemsByIndexName).map(function (indexName) {
var items = itemsByIndexName[indexName];
return {
index: indexName,
items: items,
algoliaSource: ['autocomplete']
};
});
}

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,2 @@
export * from './types';
export * from './createAlgoliaInsightsPlugin';

View File

@@ -0,0 +1,2 @@
export * from './types';
export * from './createAlgoliaInsightsPlugin';

View File

@@ -0,0 +1,2 @@
import { AlgoliaInsightsHit } from './types';
export declare function isAlgoliaInsightsHit(hit: any): hit is AlgoliaInsightsHit;

View File

@@ -0,0 +1,3 @@
export function isAlgoliaInsightsHit(hit) {
return hit.objectID && hit.__autocomplete_indexName && hit.__autocomplete_queryID;
}

View File

@@ -0,0 +1,6 @@
import type { InsightsClient } from './types';
/**
* Determines if a given insights `client` supports the optional call to `init`
* and the ability to set credentials via extra parameters when sending events.
*/
export declare function isModernInsightsClient(client: InsightsClient): boolean;

View File

@@ -0,0 +1,23 @@
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
* Determines if a given insights `client` supports the optional call to `init`
* and the ability to set credentials via extra parameters when sending events.
*/
export function isModernInsightsClient(client) {
var _split$map = (client.version || '').split('.').map(Number),
_split$map2 = _slicedToArray(_split$map, 2),
major = _split$map2[0],
minor = _split$map2[1];
/* eslint-disable @typescript-eslint/camelcase */
var v3 = major >= 3;
var v2_4 = major === 2 && minor >= 4;
var v1_10 = major === 1 && minor >= 10;
return v3 || v2_4 || v1_10;
/* eslint-enable @typescript-eslint/camelcase */
}

View File

@@ -0,0 +1,9 @@
export declare type AlgoliaInsightsHit = {
objectID: string;
__autocomplete_indexName: string;
__autocomplete_queryID: string;
__autocomplete_algoliaCredentials: {
appId: string;
apiKey: string;
};
};

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,63 @@
import { createSearchInsightsApi } from '../createSearchInsightsApi';
import type { AlgoliaInsightsHit } from './AlgoliaInsightsHit';
export declare type AutocompleteInsightsApi = ReturnType<typeof createSearchInsightsApi>;
export declare type InsightsParamsWithItems<TParams extends {
objectIDs: string[];
}> = Omit<TParams, 'objectIDs'> & {
items: AlgoliaInsightsHit[];
/**
* @deprecated use `items` instead
*/
objectIDs?: string[];
};
export declare type ClickedObjectIDsAfterSearchParams = {
eventName: string;
index: string;
objectIDs: string[];
positions: number[];
queryID: string;
userToken?: string;
};
export declare type ClickedObjectIDsParams = {
eventName: string;
index: string;
objectIDs: string[];
userToken?: string;
};
export declare type ClickedFiltersParams = {
eventName: string;
filters: string[];
index: string;
userToken: string;
};
export declare type ConvertedObjectIDsAfterSearchParams = {
eventName: string;
index: string;
objectIDs: string[];
queryID: string;
userToken?: string;
};
export declare type ConvertedObjectIDsParams = {
eventName: string;
index: string;
objectIDs: string[];
userToken: string;
};
export declare type ConvertedFiltersParams = {
eventName: string;
filters: string[];
index: string;
userToken: string;
};
export declare type ViewedObjectIDsParams = {
eventName: string;
index: string;
objectIDs: string[];
userToken?: string;
};
export declare type ViewedFiltersParams = {
eventName: string;
filters: string[];
index: string;
userToken: string;
};

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,20 @@
import type { AutocompleteState } from '@algolia/autocomplete-shared';
import type { ClickedObjectIDsAfterSearchParams, InsightsParamsWithItems, ViewedObjectIDsParams } from './AutocompleteInsightsApi';
import type { AlgoliaInsightsHit, AutocompleteInsightsApi } from '.';
export declare type OnSelectParams = {
insights: AutocompleteInsightsApi;
insightsEvents: Array<InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams & {
algoliaSource?: string[];
}>>;
item: AlgoliaInsightsHit;
state: AutocompleteState<any>;
event: any;
};
export declare type OnActiveParams = OnSelectParams;
export declare type OnItemsChangeParams = {
insights: AutocompleteInsightsApi;
insightsEvents: Array<InsightsParamsWithItems<ViewedObjectIDsParams & {
algoliaSource?: string[];
}>>;
state: AutocompleteState<any>;
};

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,16 @@
import type { InsightsMethodMap, InsightsClient as _InsightsClient } from 'search-insights';
export type { Init as InsightsInit, AddAlgoliaAgent as InsightsAddAlgoliaAgent, SetUserToken as InsightsSetUserToken, GetUserToken as InsightsGetUserToken, OnUserTokenChange as InsightsOnUserTokenChange, } from 'search-insights';
export declare type InsightsClientMethod = keyof InsightsMethodMap;
export declare type InsightsClientPayload = {
eventName: string;
queryID: string;
index: string;
objectIDs: string[];
positions?: number[];
};
declare type QueueItemMap = Record<string, unknown>;
declare type QueueItem = QueueItemMap[keyof QueueItemMap];
export declare type InsightsClient = _InsightsClient & {
queue?: QueueItem[];
version?: string;
};

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,4 @@
export * from './AlgoliaInsightsHit';
export * from './AutocompleteInsightsApi';
export * from './EventParams';
export * from './InsightsClient';

View File

@@ -0,0 +1,4 @@
export * from './AlgoliaInsightsHit';
export * from './AutocompleteInsightsApi';
export * from './EventParams';
export * from './InsightsClient';

View File

@@ -0,0 +1,631 @@
/*! @algolia/autocomplete-plugin-algolia-insights 1.9.3 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@algolia/autocomplete-plugin-algolia-insights"] = {}));
})(this, (function (exports) { 'use strict';
function _iterableToArrayLimit(arr, i) {
var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
if (null != _i) {
var _s,
_e,
_x,
_r,
_arr = [],
_n = !0,
_d = !1;
try {
if (_x = (_i = _i.call(arr)).next, 0 === i) {
if (Object(_i) !== _i) return;
_n = !1;
} else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
} catch (err) {
_d = !0, _e = err;
} finally {
try {
if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
} finally {
if (_d) throw _e;
}
}
return _arr;
}
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
enumerableOnly && (symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
})), keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = null != arguments[i] ? arguments[i] : {};
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
_defineProperty(target, key, source[key]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _defineProperty(obj, key, value) {
key = _toPropertyKey(key);
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function _objectWithoutProperties(source, excluded) {
if (source == null) return {};
var target = _objectWithoutPropertiesLoose(source, excluded);
var key, i;
if (Object.getOwnPropertySymbols) {
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
for (i = 0; i < sourceSymbolKeys.length; i++) {
key = sourceSymbolKeys[i];
if (excluded.indexOf(key) >= 0) continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
target[key] = source[key];
}
}
return target;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _toPrimitive(input, hint) {
if (typeof input !== "object" || input === null) return input;
var prim = input[Symbol.toPrimitive];
if (prim !== undefined) {
var res = prim.call(input, hint || "default");
if (typeof res !== "object") return res;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
var key = _toPrimitive(arg, "string");
return typeof key === "symbol" ? key : String(key);
}
function createRef(initialValue) {
return {
current: initialValue
};
}
function debounce(fn, time) {
var timerId = undefined;
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(function () {
return fn.apply(void 0, args);
}, time);
};
}
function isPrimitive(obj) {
return obj !== Object(obj);
}
function isEqual(first, second) {
if (first === second) {
return true;
}
if (isPrimitive(first) || isPrimitive(second) || typeof first === 'function' || typeof second === 'function') {
return first === second;
}
if (Object.keys(first).length !== Object.keys(second).length) {
return false;
}
for (var _i = 0, _Object$keys = Object.keys(first); _i < _Object$keys.length; _i++) {
var key = _Object$keys[_i];
if (!(key in second)) {
return false;
}
if (!isEqual(first[key], second[key])) {
return false;
}
}
return true;
}
var noop = function noop() {};
/**
* Safely runs code meant for browser environments only.
*/
function safelyRunOnBrowser(callback) {
if (typeof window !== 'undefined') {
return callback({
window: window
});
}
return undefined;
}
function createClickedEvent(_ref) {
var item = _ref.item,
items = _ref.items;
return {
index: item.__autocomplete_indexName,
items: [item],
positions: [1 + items.findIndex(function (x) {
return x.objectID === item.objectID;
})],
queryID: item.__autocomplete_queryID,
algoliaSource: ['autocomplete']
};
}
/**
* Determines if a given insights `client` supports the optional call to `init`
* and the ability to set credentials via extra parameters when sending events.
*/
function isModernInsightsClient(client) {
var _split$map = (client.version || '').split('.').map(Number),
_split$map2 = _slicedToArray(_split$map, 2),
major = _split$map2[0],
minor = _split$map2[1];
/* eslint-disable @typescript-eslint/camelcase */
var v3 = major >= 3;
var v2_4 = major === 2 && minor >= 4;
var v1_10 = major === 1 && minor >= 10;
return v3 || v2_4 || v1_10;
/* eslint-enable @typescript-eslint/camelcase */
}
var _excluded = ["items"],
_excluded2 = ["items"];
function chunk(item) {
var chunkSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 20;
var chunks = [];
for (var i = 0; i < item.objectIDs.length; i += chunkSize) {
chunks.push(_objectSpread2(_objectSpread2({}, item), {}, {
objectIDs: item.objectIDs.slice(i, i + chunkSize)
}));
}
return chunks;
}
function mapToInsightsParamsApi(params) {
return params.map(function (_ref) {
var items = _ref.items,
param = _objectWithoutProperties(_ref, _excluded);
return _objectSpread2(_objectSpread2({}, param), {}, {
objectIDs: (items === null || items === void 0 ? void 0 : items.map(function (_ref2) {
var objectID = _ref2.objectID;
return objectID;
})) || param.objectIDs
});
});
}
function createSearchInsightsApi(searchInsights) {
var canSendHeaders = isModernInsightsClient(searchInsights);
function sendToInsights(method, payloads, items) {
if (canSendHeaders && typeof items !== 'undefined') {
var _items$0$__autocomple = items[0].__autocomplete_algoliaCredentials,
appId = _items$0$__autocomple.appId,
apiKey = _items$0$__autocomple.apiKey;
var headers = {
'X-Algolia-Application-Id': appId,
'X-Algolia-API-Key': apiKey
};
searchInsights.apply(void 0, [method].concat(_toConsumableArray(payloads), [{
headers: headers
}]));
} else {
searchInsights.apply(void 0, [method].concat(_toConsumableArray(payloads)));
}
}
return {
/**
* Initializes Insights with Algolia credentials.
*/
init: function init(appId, apiKey) {
searchInsights('init', {
appId: appId,
apiKey: apiKey
});
},
/**
* Sets the user token to attach to events.
*/
setUserToken: function setUserToken(userToken) {
searchInsights('setUserToken', userToken);
},
/**
* Sends click events to capture a query and its clicked items and positions.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids-after-search/
*/
clickedObjectIDsAfterSearch: function clickedObjectIDsAfterSearch() {
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
params[_key] = arguments[_key];
}
if (params.length > 0) {
sendToInsights('clickedObjectIDsAfterSearch', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends click events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids/
*/
clickedObjectIDs: function clickedObjectIDs() {
for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
params[_key2] = arguments[_key2];
}
if (params.length > 0) {
sendToInsights('clickedObjectIDs', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends click events to capture the filters a user clicks on.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-filters/
*/
clickedFilters: function clickedFilters() {
for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
params[_key3] = arguments[_key3];
}
if (params.length > 0) {
searchInsights.apply(void 0, ['clickedFilters'].concat(params));
}
},
/**
* Sends conversion events to capture a query and its clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids-after-search/
*/
convertedObjectIDsAfterSearch: function convertedObjectIDsAfterSearch() {
for (var _len4 = arguments.length, params = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
params[_key4] = arguments[_key4];
}
if (params.length > 0) {
sendToInsights('convertedObjectIDsAfterSearch', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends conversion events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids/
*/
convertedObjectIDs: function convertedObjectIDs() {
for (var _len5 = arguments.length, params = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
params[_key5] = arguments[_key5];
}
if (params.length > 0) {
sendToInsights('convertedObjectIDs', mapToInsightsParamsApi(params), params[0].items);
}
},
/**
* Sends conversion events to capture the filters a user uses when converting.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-filters/
*/
convertedFilters: function convertedFilters() {
for (var _len6 = arguments.length, params = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
params[_key6] = arguments[_key6];
}
if (params.length > 0) {
searchInsights.apply(void 0, ['convertedFilters'].concat(params));
}
},
/**
* Sends view events to capture clicked items.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-object-ids/
*/
viewedObjectIDs: function viewedObjectIDs() {
for (var _len7 = arguments.length, params = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
params[_key7] = arguments[_key7];
}
if (params.length > 0) {
params.reduce(function (acc, _ref3) {
var items = _ref3.items,
param = _objectWithoutProperties(_ref3, _excluded2);
return [].concat(_toConsumableArray(acc), _toConsumableArray(chunk(_objectSpread2(_objectSpread2({}, param), {}, {
objectIDs: (items === null || items === void 0 ? void 0 : items.map(function (_ref4) {
var objectID = _ref4.objectID;
return objectID;
})) || param.objectIDs
})).map(function (payload) {
return {
items: items,
payload: payload
};
})));
}, []).forEach(function (_ref5) {
var items = _ref5.items,
payload = _ref5.payload;
return sendToInsights('viewedObjectIDs', [payload], items);
});
}
},
/**
* Sends view events to capture the filters a user uses when viewing.
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-filters/
*/
viewedFilters: function viewedFilters() {
for (var _len8 = arguments.length, params = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
params[_key8] = arguments[_key8];
}
if (params.length > 0) {
searchInsights.apply(void 0, ['viewedFilters'].concat(params));
}
}
};
}
function createViewedEvents(_ref) {
var items = _ref.items;
var itemsByIndexName = items.reduce(function (acc, current) {
var _acc$current$__autoco;
acc[current.__autocomplete_indexName] = ((_acc$current$__autoco = acc[current.__autocomplete_indexName]) !== null && _acc$current$__autoco !== void 0 ? _acc$current$__autoco : []).concat(current);
return acc;
}, {});
return Object.keys(itemsByIndexName).map(function (indexName) {
var items = itemsByIndexName[indexName];
return {
index: indexName,
items: items,
algoliaSource: ['autocomplete']
};
});
}
function isAlgoliaInsightsHit(hit) {
return hit.objectID && hit.__autocomplete_indexName && hit.__autocomplete_queryID;
}
var VIEW_EVENT_DELAY = 400;
var ALGOLIA_INSIGHTS_VERSION = '2.6.0';
var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@".concat(ALGOLIA_INSIGHTS_VERSION, "/dist/search-insights.min.js");
var sendViewedObjectIDs = debounce(function (_ref) {
var onItemsChange = _ref.onItemsChange,
items = _ref.items,
insights = _ref.insights,
state = _ref.state;
onItemsChange({
insights: insights,
insightsEvents: createViewedEvents({
items: items
}).map(function (event) {
return _objectSpread2({
eventName: 'Items Viewed'
}, event);
}),
state: state
});
}, VIEW_EVENT_DELAY);
function createAlgoliaInsightsPlugin(options) {
var _getOptions = getOptions(options),
providedInsightsClient = _getOptions.insightsClient,
onItemsChange = _getOptions.onItemsChange,
onSelectEvent = _getOptions.onSelect,
onActiveEvent = _getOptions.onActive;
var insightsClient = providedInsightsClient;
if (!providedInsightsClient) {
safelyRunOnBrowser(function (_ref2) {
var window = _ref2.window;
var pointer = window.AlgoliaAnalyticsObject || 'aa';
if (typeof pointer === 'string') {
insightsClient = window[pointer];
}
if (!insightsClient) {
window.AlgoliaAnalyticsObject = pointer;
if (!window[pointer]) {
window[pointer] = function () {
if (!window[pointer].queue) {
window[pointer].queue = [];
}
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
window[pointer].queue.push(args);
};
}
window[pointer].version = ALGOLIA_INSIGHTS_VERSION;
insightsClient = window[pointer];
loadInsights(window);
}
});
}
var insights = createSearchInsightsApi(insightsClient);
var previousItems = createRef([]);
var debouncedOnStateChange = debounce(function (_ref3) {
var state = _ref3.state;
if (!state.isOpen) {
return;
}
var items = state.collections.reduce(function (acc, current) {
return [].concat(_toConsumableArray(acc), _toConsumableArray(current.items));
}, []).filter(isAlgoliaInsightsHit);
if (!isEqual(previousItems.current.map(function (x) {
return x.objectID;
}), items.map(function (x) {
return x.objectID;
}))) {
previousItems.current = items;
if (items.length > 0) {
sendViewedObjectIDs({
onItemsChange: onItemsChange,
items: items,
insights: insights,
state: state
});
}
}
}, 0);
return {
name: 'aa.algoliaInsightsPlugin',
subscribe: function subscribe(_ref4) {
var setContext = _ref4.setContext,
onSelect = _ref4.onSelect,
onActive = _ref4.onActive;
insightsClient('addAlgoliaAgent', 'insights-plugin');
setContext({
algoliaInsightsPlugin: {
__algoliaSearchParameters: {
clickAnalytics: true
},
insights: insights
}
});
onSelect(function (_ref5) {
var item = _ref5.item,
state = _ref5.state,
event = _ref5.event;
if (!isAlgoliaInsightsHit(item)) {
return;
}
onSelectEvent({
state: state,
event: event,
insights: insights,
item: item,
insightsEvents: [_objectSpread2({
eventName: 'Item Selected'
}, createClickedEvent({
item: item,
items: previousItems.current
}))]
});
});
onActive(function (_ref6) {
var item = _ref6.item,
state = _ref6.state,
event = _ref6.event;
if (!isAlgoliaInsightsHit(item)) {
return;
}
onActiveEvent({
state: state,
event: event,
insights: insights,
item: item,
insightsEvents: [_objectSpread2({
eventName: 'Item Active'
}, createClickedEvent({
item: item,
items: previousItems.current
}))]
});
});
},
onStateChange: function onStateChange(_ref7) {
var state = _ref7.state;
debouncedOnStateChange({
state: state
});
},
__autocomplete_pluginOptions: options
};
}
function getOptions(options) {
return _objectSpread2({
onItemsChange: function onItemsChange(_ref8) {
var insights = _ref8.insights,
insightsEvents = _ref8.insightsEvents;
insights.viewedObjectIDs.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
return _objectSpread2(_objectSpread2({}, event), {}, {
algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
});
})));
},
onSelect: function onSelect(_ref9) {
var insights = _ref9.insights,
insightsEvents = _ref9.insightsEvents;
insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
return _objectSpread2(_objectSpread2({}, event), {}, {
algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
});
})));
},
onActive: noop
}, options);
}
function loadInsights(environment) {
var errorMessage = "[Autocomplete]: Could not load search-insights.js. Please load it manually following https://alg.li/insights-autocomplete";
try {
var script = environment.document.createElement('script');
script.async = true;
script.src = ALGOLIA_INSIGHTS_SRC;
script.onerror = function () {
// eslint-disable-next-line no-console
console.error(errorMessage);
};
document.body.appendChild(script);
} catch (cause) {
// eslint-disable-next-line no-console
console.error(errorMessage);
}
}
exports.createAlgoliaInsightsPlugin = createAlgoliaInsightsPlugin;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=index.development.js.map

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,39 @@
{
"name": "@algolia/autocomplete-plugin-algolia-insights",
"description": "A plugin to add Algolia Insights to Algolia Autocomplete.",
"version": "1.9.3",
"license": "MIT",
"homepage": "https://github.com/algolia/autocomplete",
"repository": "algolia/autocomplete",
"author": {
"name": "Algolia, Inc.",
"url": "https://www.algolia.com"
},
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
],
"scripts": {
"build:clean": "rm -rf ./dist",
"build:esm": "babel src --root-mode upward --extensions '.ts,.tsx' --out-dir dist/esm --ignore '**/*/__tests__/'",
"build:types": "tsc -p ./tsconfig.declaration.json --outDir ./dist/esm",
"build:umd": "rollup --config",
"build": "rm -rf ./dist && yarn build:umd && yarn build:esm && yarn build:types",
"on:change": "concurrently \"yarn build:esm\" \"yarn build:types\"",
"prepare": "yarn run build:esm && yarn build:types",
"watch": "watch \"yarn on:change\" --ignoreDirectoryPattern \"/dist/\""
},
"dependencies": {
"@algolia/autocomplete-shared": "1.9.3"
},
"peerDependencies": {
"search-insights": ">= 1 < 3"
}
}