clean-insights-sdk
Version:
A privacy-preserving measurement framework.
105 lines (104 loc) • 5.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Configuration = void 0;
var Campaign_1 = require("./Campaign");
var Configuration = /** @class */ (function () {
/**
* @param {string} server
* The server URL should look like `https://myhost.example.com/ci/cleaninsights.php`.
*
* @param {number=} siteId
* The Matomo site ID to record this data for. OPTIONAL if provided via an object as first argument.
*
* @param {Object.<string, Campaign>=} campaigns
* Campaign configuration. OPTIONAL if provided via an object as first argument.
*
* @param {number=} timeout=5
* Connection timeout in seconds. OPTIONAL.
*
* @param {number=} maxRetryDelay=3600
* The SDK uses a truncated exponential backoff strategy on server failures.
* So the delay until it retries will rise exponentially, until it reaches
* `maxRetryDelay` seconds. OPTIONAL.
*
* @param {number=} maxAgeOfOldData=100
* The number in days of how long the SDK will try to keep sending old measurements.
* If the measurements become older than that, they will be purged. OPTIONAL.
*
* @param {number=} persistEveryNTimes=10
* Regulates, how often data persistence is done. OPTIONAL.
*
* If set to 1, every time something is tracked, *ALL* data is stored to disk.
* The more you track, the higher you should set this to avoid heavy load due
* to disk I/O.
*
* @param {boolean=} serverSideAnonymousUsage=false
* When set to true, assumes consent for all campaigns and none for features.
* Only use this, when you're running on the server and don't measure anything users
* might need to give consent to!
*
* @param {boolean=} debug=false
* When set, CleanInsights SDK will print some debug output to STDOUT. OPTIONAL.
*/
function Configuration(server, siteId, campaigns, timeout, maxRetryDelay, maxAgeOfOldData, persistEveryNTimes, serverSideAnonymousUsage, debug) {
if (typeof server === 'string') {
if (!siteId || !campaigns) {
throw TypeError('You either need to provide all non-optional arguments in their place or in an object as the first argument.');
}
this.server = server;
this.siteId = siteId;
this.campaigns = campaigns;
this.timeout = typeof timeout === 'number' ? timeout : 5;
this.maxRetryDelay = typeof maxRetryDelay === 'number' ? maxRetryDelay : 3600;
this.maxAgeOfOldData = typeof maxAgeOfOldData === 'number' ? maxAgeOfOldData : 100;
this.persistEveryNTimes = typeof persistEveryNTimes === 'number' ? persistEveryNTimes : 10;
this.serverSideAnonymousUsage = serverSideAnonymousUsage || false;
this.debug = debug || false;
}
else {
this.server = server.server;
this.siteId = server.siteId;
this.campaigns = {};
for (var campaignId in server.campaigns) {
if (server.campaigns.hasOwnProperty(campaignId)) {
this.campaigns[campaignId] = new Campaign_1.Campaign(server.campaigns[campaignId]);
}
}
this.timeout = typeof server.timeout === 'number' ? server.timeout : 5;
this.maxRetryDelay = typeof server.maxRetryDelay === 'number' ? server.maxRetryDelay : 3600;
this.maxAgeOfOldData = typeof server.maxAgeOfOldData === 'number' ? server.maxAgeOfOldData : 100;
this.persistEveryNTimes = typeof server.persistEveryNTimes === 'number' ? server.persistEveryNTimes : 10;
this.serverSideAnonymousUsage = server.serverSideAnonymousUsage || false;
this.debug = server.debug || false;
}
}
Configuration.prototype.toString = function () {
return "[".concat(this.constructor.name, ": server=").concat(this.server.toString(), ", siteId=").concat(this.siteId, ", timeout=").concat(this.timeout, ", maxRetryDelay=").concat(this.maxRetryDelay, ", maxAgeOfOldData=").concat(this.maxAgeOfOldData, ", persistEveryNTimes=").concat(this.persistEveryNTimes, ", debug=").concat(this.debug, ", campaigns=").concat(this.campaigns, "]");
};
/**
* Checks configuration for some well-known problems, emits a debug message and returns false, if one found.
*
* @param {function} debug
* Function to handle the debug message.
* @return {boolean} `true`, if config seems ok, `false` if known problems exist.
*/
Configuration.prototype.check = function (debug) {
if (!this.server || !this.server.startsWith('http')) {
debug("Configuration problem: 'server' is not defined properly. It needs to be a full URL like this: 'https://example.org/cleaninsights.php'!");
return false;
}
if (!this.siteId || this.siteId < 1) {
debug("Configuration problem: 'siteId' is not defined properly. It needs to be a positive integer value!");
return false;
}
for (var c in this.campaigns) {
if (this.campaigns.hasOwnProperty(c)) {
return true;
}
}
debug("Configuration problem: No campaign defined!");
return false;
};
return Configuration;
}());
exports.Configuration = Configuration;