From 75b92dc86a763cf08b9f24da1aeca6a13f950e08 Mon Sep 17 00:00:00 2001 From: Kelvin Lu Date: Wed, 10 Mar 2021 12:28:26 -0800 Subject: [PATCH] initial types --- types/amplitude-snippet.d.ts | 0 types/base-cookie.d.ts | 9 ++ types/base64.d.ts | 8 ++ types/base64Id.d.ts | 2 + types/constants.d.ts | 37 +++++++ types/cookie.d.ts | 20 ++++ types/get-host.d.ts | 2 + types/get-location.d.ts | 2 + types/identify.d.ts | 11 ++ types/language.d.ts | 5 + types/localstorage.d.ts | 2 + types/metadata-storage.d.ts | 47 +++++++++ types/options.d.ts | 199 +++++++++++++++++++++++++++++++++++ types/revenue.d.ts | 15 +++ types/top-domain.d.ts | 2 + types/type.d.ts | 8 ++ types/utf8.d.ts | 5 + types/utils.d.ts | 34 ++++++ types/utm.d.ts | 2 + types/uuid.d.ts | 10 ++ 20 files changed, 420 insertions(+) create mode 100644 types/amplitude-snippet.d.ts create mode 100644 types/base-cookie.d.ts create mode 100644 types/base64.d.ts create mode 100644 types/base64Id.d.ts create mode 100644 types/constants.d.ts create mode 100644 types/cookie.d.ts create mode 100644 types/get-host.d.ts create mode 100644 types/get-location.d.ts create mode 100644 types/identify.d.ts create mode 100644 types/language.d.ts create mode 100644 types/localstorage.d.ts create mode 100644 types/metadata-storage.d.ts create mode 100644 types/options.d.ts create mode 100644 types/revenue.d.ts create mode 100644 types/top-domain.d.ts create mode 100644 types/type.d.ts create mode 100644 types/utf8.d.ts create mode 100644 types/utils.d.ts create mode 100644 types/utm.d.ts create mode 100644 types/uuid.d.ts diff --git a/types/amplitude-snippet.d.ts b/types/amplitude-snippet.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/types/base-cookie.d.ts b/types/base-cookie.d.ts new file mode 100644 index 00000000..d3284da4 --- /dev/null +++ b/types/base-cookie.d.ts @@ -0,0 +1,9 @@ +declare namespace _default { + export { set }; + export { get }; + export { areCookiesEnabled }; +} +export default _default; +declare function set(name: any, value: any, opts: any): void; +declare function get(name: any): string; +declare function areCookiesEnabled(opts?: {}): boolean; diff --git a/types/base64.d.ts b/types/base64.d.ts new file mode 100644 index 00000000..8ef1a897 --- /dev/null +++ b/types/base64.d.ts @@ -0,0 +1,8 @@ +export default Base64; +declare namespace Base64 { + const _keyStr: string; + function encode(input: any): string; + function _encode(input: any): string; + function decode(input: any): string; + function _decode(input: any): string; +} diff --git a/types/base64Id.d.ts b/types/base64Id.d.ts new file mode 100644 index 00000000..8b5bdaf3 --- /dev/null +++ b/types/base64Id.d.ts @@ -0,0 +1,2 @@ +export default base64Id; +declare function base64Id(): string; diff --git a/types/constants.d.ts b/types/constants.d.ts new file mode 100644 index 00000000..71fe6e64 --- /dev/null +++ b/types/constants.d.ts @@ -0,0 +1,37 @@ +declare namespace _default { + const DEFAULT_INSTANCE: string; + const API_VERSION: number; + const MAX_STRING_LENGTH: number; + const MAX_PROPERTY_KEYS: number; + const IDENTIFY_EVENT: string; + const GROUP_IDENTIFY_EVENT: string; + const LAST_EVENT_ID: string; + const LAST_EVENT_TIME: string; + const LAST_IDENTIFY_ID: string; + const LAST_SEQUENCE_NUMBER: string; + const SESSION_ID: string; + const DEVICE_ID: string; + const OPT_OUT: string; + const USER_ID: string; + const COOKIE_TEST_PREFIX: string; + const COOKIE_PREFIX: string; + const STORAGE_DEFAULT: string; + const STORAGE_COOKIES: string; + const STORAGE_NONE: string; + const STORAGE_LOCAL: string; + const STORAGE_SESSION: string; + const REVENUE_EVENT: string; + const REVENUE_PRODUCT_ID: string; + const REVENUE_QUANTITY: string; + const REVENUE_PRICE: string; + const REVENUE_REVENUE_TYPE: string; + const AMP_DEVICE_ID_PARAM: string; + const REFERRER: string; + const UTM_SOURCE: string; + const UTM_MEDIUM: string; + const UTM_CAMPAIGN: string; + const UTM_TERM: string; + const UTM_CONTENT: string; + const ATTRIBUTION_EVENT: string; +} +export default _default; diff --git a/types/cookie.d.ts b/types/cookie.d.ts new file mode 100644 index 00000000..d824dfb4 --- /dev/null +++ b/types/cookie.d.ts @@ -0,0 +1,20 @@ +declare namespace _default { + export { reset }; + export { options }; + export { get }; + export { set }; + export { remove }; + export { setRaw }; + export { getRaw }; +} +export default _default; +declare function reset(): void; +declare function options(opts: any, ...args: any[]): { + expirationDays: any; + domain: any; +}; +declare function get(name: any): any; +declare function set(name: any, value: any): boolean; +declare function remove(name: any): boolean; +declare function setRaw(name: any, value: any): boolean; +declare function getRaw(name: any): string; diff --git a/types/get-host.d.ts b/types/get-host.d.ts new file mode 100644 index 00000000..af2027de --- /dev/null +++ b/types/get-host.d.ts @@ -0,0 +1,2 @@ +export default getHost; +declare function getHost(url: any): string; diff --git a/types/get-location.d.ts b/types/get-location.d.ts new file mode 100644 index 00000000..52eef4df --- /dev/null +++ b/types/get-location.d.ts @@ -0,0 +1,2 @@ +export default getLocation; +declare function getLocation(): Location; diff --git a/types/identify.d.ts b/types/identify.d.ts new file mode 100644 index 00000000..77d0ca47 --- /dev/null +++ b/types/identify.d.ts @@ -0,0 +1,11 @@ +export default Identify; +declare class Identify { + public add(property: string, value: number | string): Identify; + public append(property: string, value: number | string | any | object): Identify; + private clearAll; + public prepend(property: string, value: number | string | any | object): Identify; + public set(property: string, value: number | string | any | boolean | object): Identify; + public setOnce(property: string, value: number | string | any | boolean | object): Identify; + public unset(property: string): Identify; + private _addOperation; +} diff --git a/types/language.d.ts b/types/language.d.ts new file mode 100644 index 00000000..1da832ec --- /dev/null +++ b/types/language.d.ts @@ -0,0 +1,5 @@ +declare namespace _default { + export { getLanguage }; +} +export default _default; +declare function getLanguage(): any; diff --git a/types/localstorage.d.ts b/types/localstorage.d.ts new file mode 100644 index 00000000..94c6367e --- /dev/null +++ b/types/localstorage.d.ts @@ -0,0 +1,2 @@ +export default localStorage; +declare var localStorage: any; diff --git a/types/metadata-storage.d.ts b/types/metadata-storage.d.ts new file mode 100644 index 00000000..ae9c6270 --- /dev/null +++ b/types/metadata-storage.d.ts @@ -0,0 +1,47 @@ +export default MetadataStorage; +/** + * MetadataStorage involves SDK data persistance + * storage priority: cookies -> localStorage -> in memory + * This priority can be overriden by setting the storage options. + * if in localStorage, unable track users between subdomains + * if in memory, then memory can't be shared between different tabs + */ +declare class MetadataStorage { + constructor({ storageKey, disableCookies, domain, secure, sameSite, expirationDays, storage }: { + storageKey: any; + disableCookies: any; + domain: any; + secure: any; + sameSite: any; + expirationDays: any; + storage: any; + }); + storageKey: any; + domain: any; + secure: any; + sameSite: any; + expirationDays: any; + cookieDomain: any; + storage: any; + getCookieStorageKey(): any; + save({ deviceId, userId, optOut, sessionId, lastEventTime, eventId, identifyId, sequenceNumber }: { + deviceId: any; + userId: any; + optOut: any; + sessionId: any; + lastEventTime: any; + eventId: any; + identifyId: any; + sequenceNumber: any; + }): void; + load(): { + deviceId: any; + userId: string; + optOut: boolean; + sessionId: number; + lastEventTime: number; + eventId: number; + identifyId: number; + sequenceNumber: number; + }; +} diff --git a/types/options.d.ts b/types/options.d.ts new file mode 100644 index 00000000..6697da7f --- /dev/null +++ b/types/options.d.ts @@ -0,0 +1,199 @@ +declare namespace _default { + export const apiEndpoint: string; + export const batchEvents: boolean; + export const cookieExpiration: number; + export const cookieName: string; + export const sameSiteCookie: string; + export const cookieForceUpgrade: boolean; + export const deferInitialization: boolean; + export const disableCookies: boolean; + export const deviceIdFromUrlParam: boolean; + export const domain: string; + export const eventUploadPeriodMillis: number; + export const eventUploadThreshold: number; + export const forceHttps: boolean; + export const includeFbclid: boolean; + export const includeGclid: boolean; + export const includeReferrer: boolean; + export const includeUtm: boolean; + export const language: any; + export const logLevel: string; + export const logAttributionCapturedEvent: boolean; + export const optOut: boolean; + export function onError(): void; + export { platform }; + export const savedMaxCount: number; + export const saveEvents: boolean; + export const saveParamsReferrerOncePerSession: boolean; + export const secureCookie: boolean; + export const sessionTimeout: number; + export const storage: string; + export namespace trackingOptions { + export const city: boolean; + export const country: boolean; + export const carrier: boolean; + export const device_manufacturer: boolean; + export const device_model: boolean; + export const dma: boolean; + export const ip_address: boolean; + const language_1: boolean; + export { language_1 as language }; + export const os_name: boolean; + export const os_version: boolean; + const platform_1: boolean; + export { platform_1 as platform }; + export const region: boolean; + export const version_name: boolean; + } + export const unsetParamsReferrerOnNewSession: boolean; + export const unsentKey: string; + export const unsentIdentifyKey: string; + export const uploadBatchSize: number; +} +export default _default; +/** + * Options used when initializing Amplitude + */ +export type Options = { + /** + * - Endpoint to send amplitude event requests to. + */ + apiEndpoint?: string; + /** + * - If `true`, then events are batched together and uploaded only when the number of unsent events is greater than or equal to eventUploadThreshold or after eventUploadPeriodMillis milliseconds have passed since the first unsent event was logged. + */ + batchEvents?: boolean; + /** + * - The number of days after which the Amplitude cookie will expire. 12 months is for GDPR compliance. + */ + cookieExpiration?: number; + /** + * - *DEPRECATED* + */ + cookieName?: string; + /** + * - Sets the SameSite flag on the amplitude cookie. Decides cookie privacy policy. + */ + sameSiteCookie?: string; + /** + * - Forces pre-v6.0.0 instances to adopt post-v6.0.0 compat cookie formats. + */ + cookieForceUpgrade?: boolean; + /** + * - If `true`, disables the core functionality of the sdk, including saving a cookie and all logging, until explicitly enabled. To enable tracking, please call `amplitude.getInstance().enableTracking()` *Note: This will not affect users who already have an amplitude cookie. The decision to track events is determined by whether or not a user has an amplitude analytics cookie. If the `cookieExpiration is manually defined to be a short lifespan, you may need to run `amplitude.getInstance().enableTracking()` upon the cookie expiring or upon logging in.* + */ + deferInitialization?: boolean; + /** + * - Disable Ampllitude cookies altogether. + */ + disableCookies?: boolean; + /** + * randomly generated UUID.] - The custom Device ID to set. *Note: This is not recommended unless you know what you are doing (e.g. you have your own system for tracking user devices).* + */ + deviceId?: string; + /** + * - If `true`, then the SDK will parse Device ID values from the URL parameter amp_device_id if available. Device IDs defined in the configuration options during init will take priority over Device IDs from URL parameters. + */ + deviceIdFromUrlParam?: boolean; + /** + * top domain of the current page's URL. ('https://amplitude.com')] - Set a custom domain for the Amplitude cookie. To include subdomains, add a preceding period, eg: `.amplitude.com`. + */ + domain?: string; + /** + * - Amount of time in milliseconds that the SDK waits before uploading events if batchEvents is true. + */ + eventUploadPeriodMillis?: number; + /** + * - Minimum number of events to batch together per request if batchEvents is true. + */ + eventUploadThreshold?: number; + /** + * - If `true`, the events will always be uploaded to HTTPS endpoint. Otherwise, it will use the embedding site's protocol. + */ + forceHttps?: boolean; + /** + * - If `true`, captures the fbclid URL parameter as well as the user's initial_fbclid via a setOnce operation. + */ + includeFbclid?: boolean; + /** + * - If `true`, captures the gclid URL parameter as well as the user's initial_gclid via a setOnce operation. + */ + includeGclid?: boolean; + /** + * - If `true`, captures the referrer and referring_domain for each session, as well as the user's initial_referrer and initial_referring_domain via a setOnce operation. + */ + includeReferrer?: boolean; + /** + * - If `true`, finds UTM parameters in the query string or the _utmz cookie, parses, and includes them as user properties on all events uploaded. This also captures initial UTM parameters for each session via a setOnce operation. + */ + includeUtm?: boolean; + /** + * language determined by the browser] - Custom language to set. + */ + language?: string; + /** + * - Level of logs to be printed in the developer console. Valid values are 'DISABLE', 'ERROR', 'WARN', 'INFO'. To learn more about the different options, see below. + */ + logLevel?: string; + /** + * - If `true`, the SDK will log an Amplitude event anytime new attribution values are captured from the user. **Note: These events count towards your event volume.** Event name being logged: [Amplitude] Attribution Captured. Event Properties that can be logged: `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`, `referrer`, `referring_domain`, `gclid`, `fbclid`. For UTM properties to be logged, `includeUtm` must be set to `true`. For the `referrer` and `referring_domain` properties to be logged, `includeReferrer` must be set to `true`. For the `gclid` property to be logged, `includeGclid` must be set to `true`. For the `fbclid` property to be logged, `includeFbclid` must be set to `true`. + */ + logAttributionCapturedEvent?: boolean; + /** + * - Whether or not to disable tracking for the current user. + */ + optOut?: boolean; + /** + * - Function to call on error. + */ + onError?: Function; + /** + * - Platform device is running on. `Web` is a browser (including mobile browsers). `iOS` and `Android` are relevant only for react-native apps. + */ + platform?: string; + /** + * - Maximum number of events to save in localStorage. If more events are logged while offline, then old events are removed. + */ + savedMaxCount?: number; + /** + * - If `true`, saves events to localStorage and removes them upon successful upload. *Note: Without saving events, events may be lost if the user navigates to another page before the events are uploaded.* + */ + saveEvents?: boolean; + /** + * - If `true`, then includeGclid, includeFbclid, includeReferrer, and includeUtm will only track their respective properties once per session. New values that come in during the middle of the user's session will be ignored. Set to false to always capture new values. + */ + saveParamsReferrerOncePerSession?: boolean; + /** + * - If `true`, the amplitude cookie will be set with the Secure flag. + */ + secureCookie?: boolean; + /** + * - The time between logged events before a new session starts in milliseconds. + */ + sessionTimeout?: number; + /** + * - Sets storage strategy. Options are 'cookies', 'localStorage', 'sessionStorage', or `none`. Will override `disableCookies` option + */ + storage?: string[]; + /** + * - Type of data associated with a user. + */ + trackingOptions?: any; + /** + * - If `false`, the existing `referrer` and `utm_parameter` values will be carried through each new session. If set to `true`, the `referrer` and `utm_parameter` user properties, which include `referrer`, `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, and `utm_content`, will be set to `null` upon instantiating a new session. Note: This only works if `includeReferrer` or `includeUtm` is set to `true`. + */ + unsetParamsReferrerOnNewSession?: boolean; + /** + * - localStorage key that stores unsent events. + */ + unsentKey?: string; + /** + * - localStorage key that stores unsent identifies. + */ + unsentIdentifyKey?: string; + /** + * - The maximum number of events to send to the server per request. + */ + uploadBatchSize?: number; +}; +declare let platform: string; diff --git a/types/revenue.d.ts b/types/revenue.d.ts new file mode 100644 index 00000000..b77cbb23 --- /dev/null +++ b/types/revenue.d.ts @@ -0,0 +1,15 @@ +export default Revenue; +declare class Revenue { + public setProductId(productId: string): Revenue; + _productId: string; + public setQuantity(quantity: number): Revenue; + _quantity: number; + public setPrice(price: number): Revenue; + _price: number; + public setRevenueType(revenueType: string): Revenue; + _revenueType: string; + public setEventProperties(eventProperties: object): Revenue; + _properties: {}; + private _isValidRevenue; + private _toJSONObject; +} diff --git a/types/top-domain.d.ts b/types/top-domain.d.ts new file mode 100644 index 00000000..8735f92b --- /dev/null +++ b/types/top-domain.d.ts @@ -0,0 +1,2 @@ +export default topDomain; +declare function topDomain(url: any): string; diff --git a/types/type.d.ts b/types/type.d.ts new file mode 100644 index 00000000..0ee810e4 --- /dev/null +++ b/types/type.d.ts @@ -0,0 +1,8 @@ +/** + * Return the type of `val`. + * @private + * @param {Mixed} val + * @return {String} + * @api public + */ +export default function _default(val: any): string; diff --git a/types/utf8.d.ts b/types/utf8.d.ts new file mode 100644 index 00000000..069c472b --- /dev/null +++ b/types/utf8.d.ts @@ -0,0 +1,5 @@ +export default UTF8; +declare namespace UTF8 { + function encode(s: any): string; + function decode(utftext: any): string; +} diff --git a/types/utils.d.ts b/types/utils.d.ts new file mode 100644 index 00000000..7de32713 --- /dev/null +++ b/types/utils.d.ts @@ -0,0 +1,34 @@ +declare namespace _default { + export { setLogLevel }; + export { getLogLevel }; + export { logLevels }; + export { log }; + export { isEmptyString }; + export { getQueryParam }; + export { sessionStorageEnabled }; + export { truncate }; + export { validateGroups }; + export { validateInput }; + export { validateProperties }; +} +export default _default; +declare function setLogLevel(logLevelName: any): void; +declare function getLogLevel(): number; +declare namespace logLevels { + const DISABLE: number; + const ERROR: number; + const WARN: number; + const INFO: number; +} +declare namespace log { + function error(s: any): void; + function warn(s: any): void; + function info(s: any): void; +} +declare function isEmptyString(str: any): boolean; +declare function getQueryParam(name: any, query: any): string; +declare function sessionStorageEnabled(): boolean; +declare function truncate(value: any): any; +declare function validateGroups(groups: any): {}; +declare function validateInput(input: any, name: any, expectedType: any): boolean; +declare function validateProperties(properties: any): {}; diff --git a/types/utm.d.ts b/types/utm.d.ts new file mode 100644 index 00000000..204cabed --- /dev/null +++ b/types/utm.d.ts @@ -0,0 +1,2 @@ +export default getUtmData; +declare function getUtmData(rawCookie: any, query: any): {}; diff --git a/types/uuid.d.ts b/types/uuid.d.ts new file mode 100644 index 00000000..3aface1d --- /dev/null +++ b/types/uuid.d.ts @@ -0,0 +1,10 @@ +export default uuid; +/** + * Source: [jed's gist]{@link https://gist.github.com/982883}. + * Returns a random v4 UUID of the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, + * where each x is replaced with a random hexadecimal digit from 0 to f, and + * y is replaced with a random hexadecimal digit from 8 to b. + * Used to generate UUIDs for deviceIds. + * @private + */ +declare function uuid(a: any): any;