Skip to content

Commit

Permalink
Refine JSDoc / TypeScript types for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
dobesv committed Oct 7, 2022
1 parent 6dad612 commit eea7d2f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
22 changes: 11 additions & 11 deletions packages/analytics-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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 +125,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 +160,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 +177,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 +275,7 @@ function analytics(config = {}) {
// Merge in custom plugin methods
...parsedOptions.methods
}

let readyCalled = false
/**
* Analytic instance returned from initialization
Expand All @@ -289,7 +289,7 @@ function analytics(config = {}) {
* @property {On} on - Fire callback on analytics lifecycle events.
* @property {Once} once - Fire callback on analytics lifecycle events once.
* @property {GetState} getState - Get data about user, activity, or context.
* @property {Storage} storage - storage methods
* @property {AnalyticsStorage} storage - storage methods
* @property {Plugins} plugins - plugin methods
*/
const instance = {
Expand Down Expand Up @@ -363,7 +363,7 @@ function analytics(config = {}) {
* Track an analytics event. This will trigger `track` calls in any installed plugins
* @typedef {Function} Track
* @param {String} eventName - Event name
* @param {Object} [payload] - Event payload
* @param {TrackData} [payload] - Event payload
* @param {Object} [options] - Event options
* @param {Function} [callback] - Callback to fire after tracking completes
* @returns {Promise}
Expand Down Expand Up @@ -733,7 +733,7 @@ function analytics(config = {}) {
/**
* Storage utilities for persisting data.
* These methods will allow you to save data in localStorage, cookies, or to the window.
* @typedef {Object} Storage
* @typedef {Object} AnalyticsStorage
* @property {GetItem} getItem - Get value from storage
* @property {SetItem} setItem - Set storage value
* @property {RemoveItem} removeItem - Remove storage value
Expand Down Expand Up @@ -887,7 +887,7 @@ function analytics(config = {}) {
}
return acc
}, {})

const initialState = {
context: initialConfig,
user: visitorInfo,
Expand Down Expand Up @@ -941,7 +941,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
2 changes: 1 addition & 1 deletion packages/analytics-core/src/modules/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function currentUrl(search) {
}

/**
* Page data for overides
* Page data for overrides
* @typedef {object} PageData
* @property {string} [title] - Page title
* @property {string} [url] - Page url
Expand Down
11 changes: 11 additions & 0 deletions packages/analytics-core/src/modules/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
import EVENTS from '../events'
import serialize from '../utils/serialize'


/**
* Track data for overrides
* @typedef {Object} TrackData
* @property {string} [hitType] (Google Analytics) https://bit.ly/2Jab9L1 one of 'pageview', 'screenview', 'event', 'transaction', 'item', 'social', 'exception', 'timing'
* @property {string} [label] (Google Analytics) event Label http://bit.ly/2oo8eb3
* @property {number} [value] (Google Analytics) event value
* @property {string} [category] (Google Analytics) event Category http://bit.ly/2EAy9UP
* @property {boolean} [nonInteraction] (Google Analytics) https://bit.ly/2CUzeoz
*/

// Track State
const initialState = {
last: {},
Expand Down
69 changes: 64 additions & 5 deletions packages/analytics-core/src/pluginTypeDef.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
/**
* @callback PluginTrackFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload event data
* @param {string} arg.payload.event event name passed to track
* @param {TrackData} arg.payload.properties event properties passed to track
* @return {void}
*/

/**
* @callback PluginPageFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @param {string} arg.payload.event
* @param {PageData} arg.payload.properties
* @return {void}
*/

/**
* @callback PluginIdentifyFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @param {string} arg.payload.event
* @param {PageData} arg.payload.properties
* @return {void}
*/

/**
* @callback PluginInitializeFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @return boolean
*/

/**
* @callback PluginLoadedFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @return boolean
*/

/**
* @callback PluginReadyFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @return void
*/

/**
* @typedef {Object} AnalyticsPlugin
* @property {string} name - Name of plugin
* @property {Object} [EVENTS] - exposed events of plugin
* @property {Object} [config] - Configuration of plugin
* @property {function} [initialize] - Load analytics scripts method
* @property {function} [page] - Page visit tracking method
* @property {function} [track] - Custom event tracking method
* @property {function} [identify] - User identify method
* @property {function} [loaded] - Function to determine if analytics script loaded
* @property {function} [ready] - Fire function when plugin ready
* @property {PluginPageFunction} [page] - Page visit tracking method
* @property {PluginTrackFunction} [track] - Custom event tracking method
* @property {PluginIdentifyFunction} [identify] - User identify method
* @property {PluginLoadedFunction} [loaded] - Function to determine if analytics script loaded
* @property {PluginReadyFunction} [ready] - Fire function when plugin ready
*/

0 comments on commit eea7d2f

Please sign in to comment.