Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

addsearch-js-client

Package Overview
Dependencies
Maintainers
3
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

addsearch-js-client - npm Package Compare versions

Comparing version 0.8.2 to 0.8.3

src/cookie.js

11

package.json
{
"name": "addsearch-js-client",
"version": "0.8.2",
"version": "0.8.3",
"description": "AddSearch API JavaScript client",

@@ -41,6 +41,7 @@ "main": "index.js",

"es6-promise": "^4.2.8",
"js-base64": "^3.6.0"
"js-base64": "^3.6.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@babel/cli": "^7.20.7",
"@babel/cli": "^7.21.0",
"@babel/core": "^7.12.9",

@@ -50,3 +51,3 @@ "@babel/preset-env": "^7.12.7",

"axios-mock-adapter": "^1.21.2",
"babel-loader": "^8.2.1",
"babel-loader": "^9.1.2",
"buffer": "^6.0.3",

@@ -61,5 +62,5 @@ "eslint": "^8.13.0",

"uglify-js": "^3.12.0",
"webpack": "^5.72.0",
"webpack": "^5.76.1",
"webpack-cli": "^4.10.0"
}
}

@@ -338,9 +338,42 @@ # AddSearch Search API Client for JavaScript

#### Set user token (for personalized search results)
#### Enable personalization tracking
Enable personalization tracking, user token will be included in every stat events as "session ID". <br/>
Set stats session ID if user token is generated by your site.
```js
client.setStatsSessionId(userToken);
```
If session is not set, a UUID is generated and stored in a cookie named 'addsearchUserToken`.
Specify the expiration date of the cookie. Default is 180.
```js
// Defaults - isEnabled: false, expirationDates: 180
client.enablePersonalizationTracking(isEnabled, expirationDates);
```
#### Allow storing AddSearch's user token in cookie
By default, the value is true. Set it to false when users reject cookie (AddSearch's cookie can be categorized as functional/analytics cookie).
```js
// Default: true
client.consentAddSearchCookie(false);
```
#### Set user token to search query (for personalized search results)
```js
// Add a user token to the search request (if personalization in use)
client.setUserToken('uuid');
client.setUserToken(userToken);
```
#### Send personalization events with search query
#### Get user token from AddSearch cookie
Get the user token which is stored in AddSearch cookie (if available).
```js
// Get a user token
client.getUserTokenInPersonalization();
```
#### Send personalization events with search query - deprecated
In personalized search, user events are typically sent to AddSearch via API and a user token

@@ -362,2 +395,4 @@ is passed with the search query (see setUserToken function).

### Other

@@ -364,0 +399,0 @@

@@ -9,4 +9,6 @@ 'use strict';

var throttle = require('./throttle');
var cookie = require('./cookie');
var API_HOSTNAME = 'api.addsearch.com';
var USER_TOKEN_COOKIE_NAME = 'addsearchUserToken';

@@ -19,2 +21,3 @@ var client = function(sitekey, privatekey) {

this.sessionId = ('a-' + (Math.random() * 100000000)).substring(0, 10);
this.userTokenInPersonalization = cookie.getCookie(USER_TOKEN_COOKIE_NAME) || util.generateUUID();

@@ -205,12 +208,21 @@ /**

this.setThrottleTime = function(delay) {this.settings.setThrottleTime(delay);}
this.setStatsSessionId = function(id) {this.sessionId = id;}
this.setStatsSessionId = function(id) {
this.sessionId = id;
this.userTokenInPersonalization = null;
}
this.getStatsSessionId = function() {return this.sessionId;}
this.enableLogicalOperators = function(enableLogicalOperators) {this.settings.enableLogicalOperators(enableLogicalOperators)}
this.setSearchOperator = function(operator) {this.settings.setSearchOperator(operator)}
this.enableLogicalOperators = function(enableLogicalOperators) {this.settings.enableLogicalOperators(enableLogicalOperators)};
this.setSearchOperator = function(operator) {this.settings.setSearchOperator(operator)};
this.sendStatsEvent = function(type, keyword, data) {
var useUserTokenInCookie = this.userTokenInPersonalization && isPersonalizationTrackingEnabled && isAddSearchCookieConsented;
if (useUserTokenInCookie && !cookie.getCookie(USER_TOKEN_COOKIE_NAME)) {
cookie.setCookie(USER_TOKEN_COOKIE_NAME, this.userTokenInPersonalization, personalizationCookieExpireDays);
}
if (type === 'search') {
let payload = {
action: 'search',
session: this.sessionId,
session: useUserTokenInCookie ? this.userTokenInPersonalization : this.sessionId,
keyword: keyword,

@@ -226,3 +238,3 @@ numberOfResults: data.numberOfResults,

action: 'click',
session: this.sessionId,
session: useUserTokenInCookie ? this.userTokenInPersonalization : this.sessionId,
keyword: keyword,

@@ -241,3 +253,25 @@ docid: data.documentId,

/*
* Personalization
*/
var isPersonalizationTrackingEnabled = false;
var isAddSearchCookieConsented = true;
var personalizationCookieExpireDays = 180;
this.getUserTokenInPersonalization = function() {
return this.userTokenInPersonalization;
};
this.enablePersonalizationTracking = function(isEnabled, cookieExpireDays) {
isPersonalizationTrackingEnabled = !!isEnabled;
if (cookieExpireDays) {
personalizationCookieExpireDays = cookieExpireDays;
}
};
this.consentAddSearchCookie = function(isEnabled) {
isAddSearchCookieConsented = !!isEnabled;
};
// Deprecated

@@ -244,0 +278,0 @@ this.searchResultClicked = function(documentId, position) {

const Buffer = require('buffer/').Buffer;
const { v4: uuidv4 } = require('uuid');

@@ -42,6 +43,11 @@ const isFunction = function(fn) {

const generateUUID = function() {
return uuidv4().replace(/-/g, '');
};
module.exports = {
isFunction,
base64,
validateSetPagingParams: validateSetPagingParams
validateSetPagingParams: validateSetPagingParams,
generateUUID
}

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc