coveo.analytics 2.32.0 → 2.33.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.mjs +32 -11
- package/dist/coveoua.browser.js +1 -1
- package/dist/coveoua.browser.js.map +1 -1
- package/dist/coveoua.debug.js +32 -11
- package/dist/coveoua.debug.js.map +1 -1
- package/dist/coveoua.js +1 -1
- package/dist/coveoua.js.map +1 -1
- package/dist/definitions/version.d.ts +1 -1
- package/dist/esm/cookieutils.d.mts +1 -0
- package/dist/esm/cookieutils.mjs +64 -0
- package/dist/esm/detector.d.mts +1 -0
- package/dist/esm/detector.mjs +39 -0
- package/dist/esm/history.d.mts +2 -0
- package/dist/esm/history.mjs +134 -0
- package/dist/esm/storage.d.mts +1 -0
- package/dist/esm/storage.mjs +55 -0
- package/dist/library.cjs +60 -11452
- package/dist/library.es.js +32 -11
- package/dist/library.js +60 -11452
- package/dist/library.mjs +60 -11452
- package/dist/react-native.es.js +32 -11
- package/package.json +13 -7
- package/src/cookieutils.spec.ts +63 -8
- package/src/cookieutils.ts +32 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const libVersion = "2.
|
|
1
|
+
export declare const libVersion = "2.33.1";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../definitions/cookieutils.js';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
class Cookie {
|
|
2
|
+
static set(name, value, expire) {
|
|
3
|
+
let expirationDate;
|
|
4
|
+
if (expire) {
|
|
5
|
+
expirationDate = new Date();
|
|
6
|
+
expirationDate.setTime(expirationDate.getTime() + expire);
|
|
7
|
+
}
|
|
8
|
+
writeCookie(name, value, expirationDate, getCookieDomain(window.location.hostname));
|
|
9
|
+
}
|
|
10
|
+
static get(name) {
|
|
11
|
+
var cookiePrefix = name + '=';
|
|
12
|
+
var cookieArray = document.cookie.split(';');
|
|
13
|
+
for (var i = 0; i < cookieArray.length; i++) {
|
|
14
|
+
var cookie = cookieArray[i];
|
|
15
|
+
cookie = cookie.replace(/^\s+/, '');
|
|
16
|
+
if (cookie.lastIndexOf(cookiePrefix, 0) === 0) {
|
|
17
|
+
return cookie.substring(cookiePrefix.length, cookie.length);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
static erase(name) {
|
|
23
|
+
Cookie.set(name, '', -1);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function getCookieDomain(host) {
|
|
27
|
+
if (host.indexOf('.') === -1) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const domainParts = host.split('.');
|
|
31
|
+
for (let domainLength = 2; domainLength <= domainParts.length; domainLength++) {
|
|
32
|
+
const domain = domainParts.slice(-domainLength).join('.');
|
|
33
|
+
if (supportsCookieDomain(domain)) {
|
|
34
|
+
return domain;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
function supportsCookieDomain(domain) {
|
|
40
|
+
const name = `__coveo_cookie_domain_test_${Math.random().toString(36).substring(2)}`;
|
|
41
|
+
try {
|
|
42
|
+
writeCookie(name, '1', undefined, domain);
|
|
43
|
+
return Cookie.get(name) === '1';
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
try {
|
|
50
|
+
writeCookie(name, '', new Date(0), domain);
|
|
51
|
+
}
|
|
52
|
+
catch { }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function writeCookie(name, value, expirationDate, domain) {
|
|
56
|
+
document.cookie =
|
|
57
|
+
`${name}=${value}` +
|
|
58
|
+
(expirationDate ? `;expires=${expirationDate.toUTCString()}` : '') +
|
|
59
|
+
(domain ? `;domain=${domain}` : '') +
|
|
60
|
+
';path=/;SameSite=Lax' +
|
|
61
|
+
(window.location.protocol === 'https:' ? ';Secure' : '');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { Cookie };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../definitions/detector.js';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
function hasWindow() {
|
|
2
|
+
return typeof window !== 'undefined';
|
|
3
|
+
}
|
|
4
|
+
function hasNavigator() {
|
|
5
|
+
return typeof navigator !== 'undefined';
|
|
6
|
+
}
|
|
7
|
+
function hasDocument() {
|
|
8
|
+
return typeof document !== 'undefined';
|
|
9
|
+
}
|
|
10
|
+
function hasLocalStorage() {
|
|
11
|
+
try {
|
|
12
|
+
return typeof localStorage !== 'undefined';
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function hasSessionStorage() {
|
|
19
|
+
try {
|
|
20
|
+
return typeof sessionStorage !== 'undefined';
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function hasCookieStorage() {
|
|
27
|
+
return hasNavigator() && navigator.cookieEnabled;
|
|
28
|
+
}
|
|
29
|
+
function hasCrypto() {
|
|
30
|
+
return typeof crypto !== 'undefined';
|
|
31
|
+
}
|
|
32
|
+
function hasCryptoRandomValues() {
|
|
33
|
+
return hasCrypto() && typeof crypto.getRandomValues !== 'undefined';
|
|
34
|
+
}
|
|
35
|
+
function hasLocation() {
|
|
36
|
+
return typeof location !== 'undefined';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { hasCookieStorage, hasCrypto, hasCryptoRandomValues, hasDocument, hasLocalStorage, hasLocation, hasNavigator, hasSessionStorage, hasWindow };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { getAvailableStorage } from './storage.mjs';
|
|
2
|
+
|
|
3
|
+
const STORE_KEY = '__coveo.analytics.history';
|
|
4
|
+
const MAX_NUMBER_OF_HISTORY_ELEMENTS = 20;
|
|
5
|
+
const MIN_THRESHOLD_FOR_DUPLICATE_VALUE = 1000 * 60;
|
|
6
|
+
const MAX_VALUE_SIZE = 75;
|
|
7
|
+
class HistoryStore {
|
|
8
|
+
constructor(store) {
|
|
9
|
+
this.store = store || getAvailableStorage();
|
|
10
|
+
}
|
|
11
|
+
addElement(elem) {
|
|
12
|
+
elem.internalTime = new Date().getTime();
|
|
13
|
+
elem = this.cropQueryElement(this.stripEmptyQuery(elem));
|
|
14
|
+
let currentHistory = this.getHistoryWithInternalTime();
|
|
15
|
+
if (currentHistory != null) {
|
|
16
|
+
if (this.isValidEntry(elem)) {
|
|
17
|
+
this.setHistory([elem].concat(currentHistory));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.setHistory([elem]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async addElementAsync(elem) {
|
|
25
|
+
elem.internalTime = new Date().getTime();
|
|
26
|
+
elem = this.cropQueryElement(this.stripEmptyQuery(elem));
|
|
27
|
+
let currentHistory = await this.getHistoryWithInternalTimeAsync();
|
|
28
|
+
if (currentHistory != null) {
|
|
29
|
+
if (this.isValidEntry(elem)) {
|
|
30
|
+
this.setHistory([elem].concat(currentHistory));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.setHistory([elem]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
getHistory() {
|
|
38
|
+
const history = this.getHistoryWithInternalTime();
|
|
39
|
+
return this.stripEmptyQueries(this.stripInternalTime(history));
|
|
40
|
+
}
|
|
41
|
+
async getHistoryAsync() {
|
|
42
|
+
const history = await this.getHistoryWithInternalTimeAsync();
|
|
43
|
+
return this.stripEmptyQueries(this.stripInternalTime(history));
|
|
44
|
+
}
|
|
45
|
+
getHistoryWithInternalTime() {
|
|
46
|
+
try {
|
|
47
|
+
const elements = this.store.getItem(STORE_KEY);
|
|
48
|
+
if (elements && typeof elements === 'string') {
|
|
49
|
+
return JSON.parse(elements);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async getHistoryWithInternalTimeAsync() {
|
|
60
|
+
try {
|
|
61
|
+
const elements = await this.store.getItem(STORE_KEY);
|
|
62
|
+
if (elements) {
|
|
63
|
+
return JSON.parse(elements);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
setHistory(history) {
|
|
74
|
+
try {
|
|
75
|
+
this.store.setItem(STORE_KEY, JSON.stringify(history.slice(0, MAX_NUMBER_OF_HISTORY_ELEMENTS)));
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
clear() {
|
|
81
|
+
try {
|
|
82
|
+
this.store.removeItem(STORE_KEY);
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
getMostRecentElement() {
|
|
88
|
+
let currentHistory = this.getHistoryWithInternalTime();
|
|
89
|
+
if (Array.isArray(currentHistory)) {
|
|
90
|
+
const sorted = currentHistory.sort((first, second) => {
|
|
91
|
+
return (second.internalTime || 0) - (first.internalTime || 0);
|
|
92
|
+
});
|
|
93
|
+
return sorted[0];
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
cropQueryElement(part) {
|
|
98
|
+
if (part.name && part.value && part.name.toLowerCase() === 'query') {
|
|
99
|
+
part.value = part.value.slice(0, MAX_VALUE_SIZE);
|
|
100
|
+
}
|
|
101
|
+
return part;
|
|
102
|
+
}
|
|
103
|
+
isValidEntry(elem) {
|
|
104
|
+
let lastEntry = this.getMostRecentElement();
|
|
105
|
+
if (lastEntry && lastEntry.value == elem.value) {
|
|
106
|
+
return ((elem.internalTime || 0) - (lastEntry.internalTime || 0) > MIN_THRESHOLD_FOR_DUPLICATE_VALUE);
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
stripInternalTime(history) {
|
|
111
|
+
if (Array.isArray(history)) {
|
|
112
|
+
return history.map((part) => {
|
|
113
|
+
const { name, time, value } = part;
|
|
114
|
+
return { name, time, value };
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
stripEmptyQuery(part) {
|
|
120
|
+
const { name, time, value } = part;
|
|
121
|
+
if (name &&
|
|
122
|
+
typeof value === 'string' &&
|
|
123
|
+
name.toLowerCase() === 'query' &&
|
|
124
|
+
value.trim() === '') {
|
|
125
|
+
return { name, time };
|
|
126
|
+
}
|
|
127
|
+
return part;
|
|
128
|
+
}
|
|
129
|
+
stripEmptyQueries(history) {
|
|
130
|
+
return history.map((part) => this.stripEmptyQuery(part));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { HistoryStore, MAX_NUMBER_OF_HISTORY_ELEMENTS, MAX_VALUE_SIZE, MIN_THRESHOLD_FOR_DUPLICATE_VALUE, STORE_KEY, HistoryStore as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../definitions/storage.js';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { hasLocalStorage, hasCookieStorage, hasSessionStorage } from './detector.mjs';
|
|
2
|
+
import { Cookie } from './cookieutils.mjs';
|
|
3
|
+
|
|
4
|
+
let preferredStorage = null;
|
|
5
|
+
function getAvailableStorage() {
|
|
6
|
+
if (hasLocalStorage()) {
|
|
7
|
+
return localStorage;
|
|
8
|
+
}
|
|
9
|
+
if (hasCookieStorage()) {
|
|
10
|
+
return new CookieStorage();
|
|
11
|
+
}
|
|
12
|
+
if (hasSessionStorage()) {
|
|
13
|
+
return sessionStorage;
|
|
14
|
+
}
|
|
15
|
+
return new NullStorage();
|
|
16
|
+
}
|
|
17
|
+
class CookieStorage {
|
|
18
|
+
getItem(key) {
|
|
19
|
+
return Cookie.get(`${CookieStorage.prefix}${key}`);
|
|
20
|
+
}
|
|
21
|
+
removeItem(key) {
|
|
22
|
+
Cookie.erase(`${CookieStorage.prefix}${key}`);
|
|
23
|
+
}
|
|
24
|
+
setItem(key, data, expire) {
|
|
25
|
+
Cookie.set(`${CookieStorage.prefix}${key}`, data, expire);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
CookieStorage.prefix = 'coveo_';
|
|
29
|
+
class CookieAndLocalStorage {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.cookieStorage = new CookieStorage();
|
|
32
|
+
}
|
|
33
|
+
getItem(key) {
|
|
34
|
+
return localStorage.getItem(key) || this.cookieStorage.getItem(key);
|
|
35
|
+
}
|
|
36
|
+
removeItem(key) {
|
|
37
|
+
this.cookieStorage.removeItem(key);
|
|
38
|
+
localStorage.removeItem(key);
|
|
39
|
+
}
|
|
40
|
+
setItem(key, data) {
|
|
41
|
+
localStorage.setItem(key, data);
|
|
42
|
+
this.cookieStorage.setItem(key, data, 31556926000);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class NullStorage {
|
|
46
|
+
getItem(key) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
removeItem(key) {
|
|
50
|
+
}
|
|
51
|
+
setItem(key, data) {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { CookieAndLocalStorage, CookieStorage, NullStorage, getAvailableStorage, preferredStorage };
|