import { AnalyticsEvent } from './_sendEvent'; export interface ClickReport extends AnalyticsEvent { objectID: string | number; position: number; queryID?: string; } /** * Sends a click report * @param params: ClickReport */ export function click(params: ClickReport) { if(!this._hasCredentials) { throw new Error('Before calling any methods on the analytics, you first need to call the \'init\' function with applicationID and apiKey parameters'); } else if(!params) { throw new Error('No params were sent to click function, please provide an objectID and position to be reported') } else if(!params.objectID) { throw new Error('required objectID parameter was not sent, click event can not be properly attributed') } else if(!params.position) { throw new Error('required position parameter was not sent, click event position can not be properly sent without') } // Get last queryID const queryID = params.queryID || (typeof this.getQueryID === 'function' ? this.getQueryID() : this._lastQueryID); // Abort if no queryID if(!queryID) { throw new Error(`Could not retrieve queryID, please check the implementation and provide either a getQueryID function or calls the search method that will return the queryID`) } // Store click to localstorage this.storageManager.storeClick(params.objectID, queryID); // Merge queryID to params const clickParams = Object.assign({}, params, { queryID }) // Send event this.sendEvent('click', clickParams); // Return clickParams return clickParams; }