Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions packages/analytics-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import './pluginTypeDef'
* @param {string} [config.app] - Name of site / app
* @param {string} [config.version] - Version of your app
* @param {boolean} [config.debug] - Should analytics run in debug mode
* @param {boolean} [config.initialUser] - User information already known on initialization
* @param {string} [config.initialUser.userId] - User ID already known on initialization
* @param {string} [config.initialUser.anonymousId] - Anonymous ID (session ID) already known on initialization
* @param {object} [config.initialUser.traits] - User traits alreayd know on initialization
* @param {Array.<AnalyticsPlugin>} [config.plugins] - Array of analytics plugins
* @return {AnalyticsInstance} Analytics Instance
* @example
Expand Down Expand Up @@ -62,7 +66,7 @@ function analytics(config = {}) {
// if (SERVER) {
// console.log('INIT SERVER')
// }

/* Parse plugins array */
const parsedOptions = (config.plugins || []).reduce((acc, plugin) => {
if (isFunction(plugin)) {
Expand Down Expand Up @@ -125,7 +129,7 @@ function analytics(config = {}) {
middlewares: [],
events: []
})

/* Storage by default is set to global & is not persisted */
const storage = (config.storage) ? config.storage : {
getItem: get,
Expand Down Expand Up @@ -160,7 +164,7 @@ function analytics(config = {}) {
// throw new Error(`${ERROR_URL}3`)
throw new Error('Abort disabled inListener')
}

// Parse URL parameters
const params = paramsParse()
// Initialize visitor information
Expand All @@ -177,8 +181,8 @@ function analytics(config = {}) {
}

/**
* Async Management methods for plugins.
*
* Async Management methods for plugins.
*
* This is also where [custom methods](https://bit.ly/329vFXy) are loaded into the instance.
* @typedef {Object} Plugins
* @property {EnablePlugin} enable - Set storage value
Expand Down Expand Up @@ -275,7 +279,7 @@ function analytics(config = {}) {
// Merge in custom plugin methods
...parsedOptions.methods
}

let readyCalled = false
/**
* Analytic instance returned from initialization
Expand All @@ -291,12 +295,15 @@ function analytics(config = {}) {
* @property {GetState} getState - Get data about user, activity, or context.
* @property {Storage} storage - storage methods
* @property {Plugins} plugins - plugin methods
* @property {SetAnonymousId} setAnonymousId - set anonymousId value for user
*/

const instance = {
/**
* Identify a user. This will trigger `identify` calls in any installed plugins and will set user data in localStorage
* @typedef {Function} Identify
* @param {String} userId - Unique ID of user
* @param {String} anonymousId - Anonymous ID (session ID) of user
* @param {Object} [traits] - Object of user traits
* @param {Object} [options] - Options to pass to identify call
* @param {Function} [callback] - Callback function after identify completes
Expand Down Expand Up @@ -802,9 +809,11 @@ function analytics(config = {}) {
})
},
},
/*
/**
* Set the anonymous ID of the visitor
* @param {String} anonymousId - anonymous Id to set
*
* @typedef {Function} SetAnonymousId
* @param {String} anonymousId
* @param {Object} [options] - storage options
*
* @example
Expand Down Expand Up @@ -887,7 +896,7 @@ function analytics(config = {}) {
}
return acc
}, {})

const initialState = {
context: initialConfig,
user: visitorInfo,
Expand Down Expand Up @@ -941,7 +950,7 @@ function analytics(config = {}) {

const enabledPlugins = pluginKeys.filter((name) => parsedOptions.pluginEnabled[name])
const disabledPlugins = pluginKeys.filter((name) => !parsedOptions.pluginEnabled[name])

/* Register analytic plugins */
store.dispatch({
type: EVENTS.registerPlugins,
Expand Down