@amplitude/ampli
Version:
Amplitude CLI
177 lines (176 loc) • 6.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ampli = exports.Ampli = exports.UserSignedIn = exports.CommandRan = exports.CommandFailed = exports.Group = exports.Identify = exports.DefaultOptions = exports.ApiKey = void 0;
const identify_1 = require("@amplitude/identify");
const node_1 = require("@amplitude/node");
exports.ApiKey = {
production: 'c6689114f64e24bf892e9c14b58703d1',
development: '83cdd48b8c379f448c439e341ec79e7a'
};
exports.DefaultOptions = Object.assign({ plan: {
version: '158',
branch: 'main',
source: 'cli',
versionId: '8ec607b1-0b09-4251-af84-95914f8e57e8'
} }, {
ingestionMetadata: {
source_name: 'node.js-typescript-ampli',
source_version: '1.0.0'
}
});
class Identify {
constructor(event_properties) {
this.event_properties = event_properties;
this.event_type = 'Identify';
this.event_properties = event_properties;
}
}
exports.Identify = Identify;
class Group {
constructor(event_properties) {
this.event_properties = event_properties;
this.event_type = 'Group';
this.event_properties = event_properties;
}
}
exports.Group = Group;
class CommandFailed {
constructor(event_properties) {
this.event_properties = event_properties;
this.event_type = 'Command Failed';
this.event_properties = event_properties;
}
}
exports.CommandFailed = CommandFailed;
class CommandRan {
constructor(event_properties) {
this.event_properties = event_properties;
this.event_type = 'Command Ran';
this.event_properties = event_properties;
}
}
exports.CommandRan = CommandRan;
class UserSignedIn {
constructor(event_properties) {
this.event_properties = event_properties;
this.event_type = 'User Signed In';
this.event_properties = event_properties;
}
}
exports.UserSignedIn = UserSignedIn;
const getDefaultPromiseResponse = () => Promise.resolve({
status: node_1.Status.Skipped,
statusCode: 200,
});
function getIdentifyEvent(amplitudeIdentify, userId, deviceId) {
const identifyEvent = amplitudeIdentify.identifyUser('tmp-user-id-to-pass-validation');
identifyEvent.user_id = userId;
identifyEvent.device_id = deviceId;
return identifyEvent;
}
class Ampli {
constructor() {
this.disabled = false;
}
get client() {
this.isInitializedAndEnabled();
return this.amplitude;
}
get isLoaded() {
return this.amplitude != null;
}
isInitializedAndEnabled() {
if (!this.isLoaded) {
console.error('ERROR: Ampli is not yet initialized. Have you called ampli.load() on app start?');
return false;
}
return !this.disabled;
}
load(options) {
var _a, _b;
this.disabled = (_a = options.disabled) !== null && _a !== void 0 ? _a : false;
if (this.isLoaded) {
console.warn('WARNING: Ampli is already initialized. Ampli.load() should be called once at application startup.');
return;
}
let apiKey = null;
if (options.client && 'apiKey' in options.client) {
apiKey = options.client.apiKey;
}
else if ('environment' in options) {
apiKey = exports.ApiKey[options.environment];
}
if (options.client && 'instance' in options.client) {
this.amplitude = options.client.instance;
}
else if (apiKey) {
this.amplitude = node_1.init(apiKey, Object.assign(Object.assign({}, exports.DefaultOptions), (_b = options.client) === null || _b === void 0 ? void 0 : _b.options));
}
else {
console.error("ERROR: ampli.load() requires 'environment', 'client.apiKey', or 'client.instance'");
}
}
identify(userId, properties, options, extra) {
const identify = new identify_1.Identify();
const eventProperties = properties;
if (eventProperties != null) {
for (const [key, value] of Object.entries(eventProperties)) {
if (value !== undefined) {
identify.set(key, value);
}
}
}
const identifyEvent = getIdentifyEvent(identify, userId || (options === null || options === void 0 ? void 0 : options.user_id), options === null || options === void 0 ? void 0 : options.device_id);
const promise = this.isInitializedAndEnabled()
? this.amplitude.logEvent(Object.assign(Object.assign({}, options), identifyEvent), extra)
: getDefaultPromiseResponse();
return { promise };
}
setGroup(userId, name, value, options, extra) {
const identify = new identify_1.Identify().setGroup(name, value);
const identifyEvent = getIdentifyEvent(identify, userId || (options === null || options === void 0 ? void 0 : options.user_id), options === null || options === void 0 ? void 0 : options.device_id);
const promise = this.isInitializedAndEnabled()
? this.amplitude.logEvent(Object.assign(Object.assign({}, options), identifyEvent), extra)
: getDefaultPromiseResponse();
return { promise };
}
groupIdentify(groupType, groupName, properties, options, extra) {
const identify = new identify_1.Identify();
const eventProperties = properties;
if (eventProperties != null) {
for (const [key, value] of Object.entries(eventProperties)) {
if (value !== undefined) {
identify.set(key, value);
}
}
}
const groupIdentifyEvent = identify.identifyGroup(groupType, groupName);
const promise = this.isInitializedAndEnabled()
? this.amplitude.logEvent(Object.assign(Object.assign({}, options), groupIdentifyEvent), extra)
: getDefaultPromiseResponse();
return { promise };
}
track(userId, event, options, extra) {
const promise = this.isInitializedAndEnabled()
? this.amplitude.logEvent(Object.assign(Object.assign(Object.assign({}, options), event), { user_id: userId }), extra)
: getDefaultPromiseResponse();
return { promise };
}
flush() {
const promise = this.isInitializedAndEnabled()
? this.amplitude.flush()
: getDefaultPromiseResponse();
return { promise };
}
commandFailed(userId, properties, options, extra) {
return this.track(userId, new CommandFailed(properties), options, extra);
}
commandRan(userId, properties, options, extra) {
return this.track(userId, new CommandRan(properties), options, extra);
}
userSignedIn(userId, properties, options, extra) {
return this.track(userId, new UserSignedIn(properties), options, extra);
}
}
exports.Ampli = Ampli;
exports.ampli = new Ampli();