New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

analytics-client

Package Overview
Dependencies
Maintainers
1
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

analytics-client - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0-url-session-id-e1044d2af608a5db024c0ffa059d4d2e0ca9bf5f

5

CHANGELOG.md

@@ -7,2 +7,7 @@ # Change Log

# v1.3.0
## (2021-02-23)
* Implement session ID parsing and logic [Pranas Ziaukas]
# v1.2.0

@@ -9,0 +14,0 @@ ## (2021-02-22)

2

dist/package.json
{
"name": "analytics-client",
"version": "1.2.0",
"version": "1.3.0-url-session-id-e1044d2af608a5db024c0ffa059d4d2e0ca9bf5f",
"description": "Convenient builders to compose analytics tools",

@@ -5,0 +5,0 @@ "repository": {

@@ -12,2 +12,3 @@ import amplitude = require('amplitude-js');

deviceId(): string;
sessionId(): number;
regenerateDeviceId(): void;

@@ -17,2 +18,3 @@ linkDevices(userId: string, deviceIds: string[]): void;

setDeviceId(deviceId: string): void;
setSessionId(sessionId: number): void;
setUserId(userId: string): void;

@@ -19,0 +21,0 @@ setUserProperties(props: UserProperties): void;

@@ -63,5 +63,11 @@ "use strict";

};
DefaultClient.prototype.sessionId = function () {
return this.amplitudeInstance.getSessionId();
};
DefaultClient.prototype.setDeviceId = function (deviceId) {
this.amplitudeInstance.setDeviceId(deviceId);
};
DefaultClient.prototype.setSessionId = function (sessionId) {
this.amplitudeInstance.setSessionId(sessionId);
};
DefaultClient.prototype.regenerateDeviceId = function () {

@@ -134,2 +140,5 @@ this.amplitudeInstance.regenerateDeviceId();

};
NoopClient.prototype.sessionId = function () {
return -1;
};
NoopClient.prototype.linkDevices = function () {

@@ -141,2 +150,4 @@ };

};
NoopClient.prototype.setSessionId = function () {
};
NoopClient.prototype.setUserId = function () {

@@ -143,0 +154,0 @@ };

export declare const URL_PARAM_DEVICE_ID = "d_id";
export declare const URL_PARAM_SESSION_ID = "s_id";
export declare const URL_PARAM_OPT_OUT_REQUEST = "optOutAnalytics";
export declare const COOKIES_DEVICE_IDS = "__analytics_dids";
export declare const COOKIES_SESSION_ID = "__analytics_sid";
export declare const COOKIES_TTL_DAYS = 300;
export declare const USER_PROP_COMPONENT_NAME = "ComponentName";
export declare const USER_PROP_ANALYTICS_CLIENT_VERSION = "AnalyticsClientVersion";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.USER_PROP_ANALYTICS_CLIENT_VERSION = exports.USER_PROP_COMPONENT_NAME = exports.COOKIES_TTL_DAYS = exports.COOKIES_DEVICE_IDS = exports.URL_PARAM_OPT_OUT_REQUEST = exports.URL_PARAM_DEVICE_ID = void 0;
exports.USER_PROP_ANALYTICS_CLIENT_VERSION = exports.USER_PROP_COMPONENT_NAME = exports.COOKIES_TTL_DAYS = exports.COOKIES_SESSION_ID = exports.COOKIES_DEVICE_IDS = exports.URL_PARAM_OPT_OUT_REQUEST = exports.URL_PARAM_SESSION_ID = exports.URL_PARAM_DEVICE_ID = void 0;
exports.URL_PARAM_DEVICE_ID = 'd_id';
exports.URL_PARAM_SESSION_ID = 's_id';
exports.URL_PARAM_OPT_OUT_REQUEST = 'optOutAnalytics';
exports.COOKIES_DEVICE_IDS = '__analytics_dids';
exports.COOKIES_SESSION_ID = '__analytics_sid';
exports.COOKIES_TTL_DAYS = 300;

@@ -8,0 +10,0 @@ exports.USER_PROP_COMPONENT_NAME = 'ComponentName';

@@ -6,5 +6,7 @@ import { Client } from './client';

private passedDeviceId;
private sessionId;
private optOutRequsted;
constructor(client?: Client | undefined);
private setDeviceIds;
private setSessionId;
clearCookies(): void;

@@ -15,4 +17,7 @@ consumeUrlParameters(queryString: string): string | null;

getPassedDeviceId(): string | undefined;
getSessionId(): number | null;
getDeviceIdsQueryString(): string;
getSessionIdQueryString(): string;
getQueryString(): string;
isOptOutRequested(): boolean;
}

@@ -11,5 +11,8 @@ "use strict";

this.deviceIds = new Set();
this.sessionId = null;
this.optOutRequsted = false;
var storedValue = Cookies.get(config_1.COOKIES_DEVICE_IDS);
this.setDeviceIds(storedValue, null);
var storedDeviceIdValue = Cookies.get(config_1.COOKIES_DEVICE_IDS);
this.setDeviceIds(storedDeviceIdValue, null);
var storedSessionIdValue = Cookies.get(config_1.COOKIES_SESSION_ID);
this.setSessionId(storedSessionIdValue);
}

@@ -31,4 +34,15 @@ AnalyticsUrlParams.prototype.setDeviceIds = function (inputIdString, currentDeviceId) {

};
AnalyticsUrlParams.prototype.setSessionId = function (storedValue) {
if (storedValue) {
this.sessionId = Number(storedValue);
Cookies.set(config_1.COOKIES_SESSION_ID, storedValue, {
expires: config_1.COOKIES_TTL_DAYS,
path: '/',
});
}
return this.sessionId;
};
AnalyticsUrlParams.prototype.clearCookies = function () {
Cookies.remove(config_1.COOKIES_DEVICE_IDS);
Cookies.remove(config_1.COOKIES_SESSION_ID);
};

@@ -46,7 +60,13 @@ AnalyticsUrlParams.prototype.consumeUrlParameters = function (queryString) {

params.delete(config_1.URL_PARAM_DEVICE_ID);
return params.toString();
}
else {
return null;
var passedSessionId = params.get(config_1.URL_PARAM_SESSION_ID);
if (passedSessionId) {
var newCurrentSessionId = this.setSessionId(passedSessionId);
if (this.client != null && newCurrentSessionId != null) {
this.client.setSessionId(newCurrentSessionId);
}
params.delete(config_1.URL_PARAM_SESSION_ID);
}
var remainingQueryString = params.toString();
return queryString === remainingQueryString ? null : remainingQueryString;
};

@@ -61,2 +81,3 @@ AnalyticsUrlParams.prototype.setClient = function (client) {

}
this.sessionId = client.sessionId();
this.client = client;

@@ -76,2 +97,5 @@ };

};
AnalyticsUrlParams.prototype.getSessionId = function () {
return this.client ? this.client.sessionId() : this.sessionId;
};
AnalyticsUrlParams.prototype.getDeviceIdsQueryString = function () {

@@ -84,2 +108,14 @@ var ids = this.allDeviceIds();

};
AnalyticsUrlParams.prototype.getSessionIdQueryString = function () {
var id = this.getSessionId();
if (id === null) {
return '';
}
return config_1.URL_PARAM_SESSION_ID + "=" + id;
};
AnalyticsUrlParams.prototype.getQueryString = function () {
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()]
.filter(function (x) { return x; })
.join('&');
};
AnalyticsUrlParams.prototype.isOptOutRequested = function () {

@@ -86,0 +122,0 @@ return this.optOutRequsted;

@@ -11,2 +11,23 @@ "use strict";

});
test('remove session ID from the query string', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
var newQueryString = urlParams.consumeUrlParameters('s_id=123&other=value');
expect(newQueryString).toBe('other=value');
});
test('remove all relevant IDs from the query string', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
var newQueryString = urlParams.consumeUrlParameters('d_id=d1,d2,d3&s_id=123&other=value');
expect(newQueryString).toBe('other=value');
});
test('accept session ID from the query string', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getSessionId()).toBe(123);
});
test('overwrite session ID from the query string', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
urlParams.consumeUrlParameters('s_id=123&other=value');
urlParams.consumeUrlParameters('s_id=234&other=value');
expect(urlParams.getSessionId()).toBe(234);
});
test('merge device IDs', function () {

@@ -31,2 +52,16 @@ var urlParams = new url_params_1.AnalyticsUrlParams();

});
test('session ID query string', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
expect(urlParams.getSessionIdQueryString()).toBe('');
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getSessionIdQueryString()).toBe('s_id=123');
});
test('full query string', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
expect(urlParams.getQueryString()).toBe('');
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getQueryString()).toBe('s_id=123');
urlParams.consumeUrlParameters('d_id=d1&other=value');
expect(urlParams.getQueryString()).toBe('d_id=d1&s_id=123');
});
test('not changing current URL', function () {

@@ -41,2 +76,3 @@ var urlParams = new url_params_1.AnalyticsUrlParams();

deviceIdRetrieved: false,
knownSessionId: 123,
deviceId: function () {

@@ -46,5 +82,11 @@ this.deviceIdRetrieved = true;

},
sessionId: function () {
return this.knownSessionId;
},
setDeviceId: function (deviceId) {
this.setDeviceIdParams = deviceId;
},
setSessionId: function (sessionId) {
this.knownSessionId = sessionId;
},
});

@@ -70,7 +112,9 @@ };

var _a = clientUrlParameters(), urlParams = _a[0], mock = _a[1];
urlParams.consumeUrlParameters('d_id=test_input&other=value');
urlParams.consumeUrlParameters('d_id=test_input&s_id=234&other=value');
['test_input', 'test_device_id'].forEach(function (id) {
return expect(urlParams.allDeviceIds()).toContain(id);
});
expect(urlParams.getSessionId()).toBe(234);
expect(mock.setDeviceIdParams).toStrictEqual('test_input');
expect(mock.knownSessionId).toStrictEqual(234);
});

@@ -77,0 +121,0 @@ test('use first device id for analytics client', function () {

{
"name": "analytics-client",
"version": "1.2.0",
"version": "1.3.0-url-session-id-e1044d2af608a5db024c0ffa059d4d2e0ca9bf5f",
"description": "Convenient builders to compose analytics tools",

@@ -5,0 +5,0 @@ "repository": {

@@ -30,2 +30,5 @@ import amplitude = require('amplitude-js');

/** Return the ID used to identify the current session. */
sessionId(): number;
/** Generate a new device identifier used for reporting. */

@@ -43,2 +46,5 @@ regenerateDeviceId(): void;

/** Set current session ID. */
setSessionId(sessionId: number): void;
/** Set current user ID. */

@@ -139,2 +145,6 @@ setUserId(userId: string): void;

sessionId(): number {
return this.amplitudeInstance.getSessionId();
}
setDeviceId(deviceId: string) {

@@ -144,2 +154,6 @@ this.amplitudeInstance.setDeviceId(deviceId);

setSessionId(sessionId: number) {
this.amplitudeInstance.setSessionId(sessionId);
}
regenerateDeviceId() {

@@ -220,2 +234,6 @@ this.amplitudeInstance.regenerateDeviceId();

sessionId() {
return -1;
}
linkDevices() {

@@ -230,2 +248,5 @@ /* nothing */

}
setSessionId(): void {
/* nothing */
}
setUserId(): void {

@@ -232,0 +253,0 @@ /* nothing */

@@ -1,3 +0,4 @@

/** Query parameter name used to pass device ID between sites. */
/** Query parameter names used to pass IDs between sites. */
export const URL_PARAM_DEVICE_ID = 'd_id';
export const URL_PARAM_SESSION_ID = 's_id';
/**

@@ -10,2 +11,3 @@ * Query parameter name used to inform the target website that user behaviour should not be tracked.

export const COOKIES_DEVICE_IDS = '__analytics_dids';
export const COOKIES_SESSION_ID = '__analytics_sid';
export const COOKIES_TTL_DAYS = 300;

@@ -12,0 +14,0 @@

@@ -5,5 +5,7 @@ import * as Cookies from 'js-cookie';

COOKIES_DEVICE_IDS,
COOKIES_SESSION_ID,
COOKIES_TTL_DAYS,
URL_PARAM_DEVICE_ID,
URL_PARAM_OPT_OUT_REQUEST,
URL_PARAM_SESSION_ID,
} from './config';

@@ -20,7 +22,11 @@

private passedDeviceId: string | undefined;
private sessionId: number | null = null;
private optOutRequsted: boolean = false;
constructor(private client?: Client) {
const storedValue = Cookies.get(COOKIES_DEVICE_IDS);
this.setDeviceIds(storedValue, null);
const storedDeviceIdValue = Cookies.get(COOKIES_DEVICE_IDS);
this.setDeviceIds(storedDeviceIdValue, null);
const storedSessionIdValue = Cookies.get(COOKIES_SESSION_ID);
this.setSessionId(storedSessionIdValue);
}

@@ -48,8 +54,20 @@

private setSessionId(storedValue: string | null | undefined) {
if (storedValue) {
this.sessionId = Number(storedValue);
Cookies.set(COOKIES_SESSION_ID, storedValue, {
expires: COOKIES_TTL_DAYS,
path: '/',
});
}
return this.sessionId;
}
clearCookies() {
Cookies.remove(COOKIES_DEVICE_IDS);
Cookies.remove(COOKIES_SESSION_ID);
}
/**
* Analyzes the query string and stores the anonymous device ID if it's there.
* Analyzes the query string and stores the anonymous device or session ID if it's there.
* Returns null if the current URL query string should not be modified after consuming the data.

@@ -79,6 +97,17 @@ *

params.delete(URL_PARAM_DEVICE_ID);
return params.toString();
} else {
return null;
}
const passedSessionId = params.get(URL_PARAM_SESSION_ID);
if (passedSessionId) {
const newCurrentSessionId = this.setSessionId(passedSessionId);
if (this.client != null && newCurrentSessionId != null) {
this.client.setSessionId(newCurrentSessionId);
}
params.delete(URL_PARAM_SESSION_ID);
}
const remainingQueryString = params.toString();
return queryString === remainingQueryString ? null : remainingQueryString;
}

@@ -94,2 +123,3 @@

}
this.sessionId = client.sessionId();
this.client = client;

@@ -119,4 +149,11 @@ }

/**
* @return part of a query parameter string that can be appended to URLs
* @return session ID that can be passed to other sites
*/
getSessionId() {
return this.client ? this.client.sessionId() : this.sessionId;
}
/**
* @return part of the device ID query parameter string that can be appended to URLs
*/
getDeviceIdsQueryString(): string {

@@ -131,2 +168,22 @@ const ids = this.allDeviceIds();

/**
* @return part of the session ID query parameter string that can be appended to URLs
*/
getSessionIdQueryString(): string {
const id = this.getSessionId();
if (id === null) {
return '';
}
return `${URL_PARAM_SESSION_ID}=${id}`;
}
/**
* @return full query parameter string that can be appended to URLs
*/
getQueryString(): string {
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()]
.filter(x => x)
.join('&');
}
/**
* Use after consumeUrlParameters call.

@@ -133,0 +190,0 @@ * @return whether opt out from user behaviour tracking has been requested with a URL parameter

@@ -16,2 +16,37 @@ import { Client, createNoopClient } from '../src/client';

test('remove session ID from the query string', () => {
const urlParams = new AnalyticsUrlParams();
const newQueryString = urlParams.consumeUrlParameters('s_id=123&other=value');
expect(newQueryString).toBe('other=value');
});
test('remove all relevant IDs from the query string', () => {
const urlParams = new AnalyticsUrlParams();
const newQueryString = urlParams.consumeUrlParameters(
'd_id=d1,d2,d3&s_id=123&other=value',
);
expect(newQueryString).toBe('other=value');
});
test('accept session ID from the query string', () => {
const urlParams = new AnalyticsUrlParams();
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getSessionId()).toBe(123);
});
test('overwrite session ID from the query string', () => {
const urlParams = new AnalyticsUrlParams();
urlParams.consumeUrlParameters('s_id=123&other=value');
urlParams.consumeUrlParameters('s_id=234&other=value');
expect(urlParams.getSessionId()).toBe(234);
});
test('merge device IDs', () => {

@@ -46,2 +81,21 @@ const urlParams = new AnalyticsUrlParams();

test('session ID query string', () => {
const urlParams = new AnalyticsUrlParams();
expect(urlParams.getSessionIdQueryString()).toBe('');
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getSessionIdQueryString()).toBe('s_id=123');
});
test('full query string', () => {
const urlParams = new AnalyticsUrlParams();
expect(urlParams.getQueryString()).toBe('');
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getQueryString()).toBe('s_id=123');
urlParams.consumeUrlParameters('d_id=d1&other=value');
expect(urlParams.getQueryString()).toBe('d_id=d1&s_id=123');
});
test('not changing current URL', () => {

@@ -56,2 +110,3 @@ const urlParams = new AnalyticsUrlParams();

deviceIdRetrieved: boolean;
knownSessionId: number | null;
}

@@ -63,2 +118,3 @@

deviceIdRetrieved: false,
knownSessionId: 123,

@@ -69,5 +125,11 @@ deviceId() {

},
sessionId() {
return this.knownSessionId;
},
setDeviceId(deviceId: string) {
this.setDeviceIdParams = deviceId;
},
setSessionId(sessionId: number) {
this.knownSessionId = sessionId;
},
} as Client & AnalyticsMock);

@@ -99,3 +161,3 @@

urlParams.consumeUrlParameters('d_id=test_input&other=value');
urlParams.consumeUrlParameters('d_id=test_input&s_id=234&other=value');

@@ -105,4 +167,6 @@ ['test_input', 'test_device_id'].forEach(id =>

);
expect(urlParams.getSessionId()).toBe(234);
expect(mock.setDeviceIdParams).toStrictEqual('test_input');
expect(mock.knownSessionId).toStrictEqual(234);
});

@@ -109,0 +173,0 @@

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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