Skip to content

Commit 95a7fa5

Browse files
committed
Refine JSDoc / TypeScript types for plugins
1 parent 6dad612 commit 95a7fa5

File tree

4 files changed

+76
-17
lines changed

4 files changed

+76
-17
lines changed

packages/analytics-core/src/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function analytics(config = {}) {
6262
// if (SERVER) {
6363
// console.log('INIT SERVER')
6464
// }
65-
65+
6666
/* Parse plugins array */
6767
const parsedOptions = (config.plugins || []).reduce((acc, plugin) => {
6868
if (isFunction(plugin)) {
@@ -125,7 +125,7 @@ function analytics(config = {}) {
125125
middlewares: [],
126126
events: []
127127
})
128-
128+
129129
/* Storage by default is set to global & is not persisted */
130130
const storage = (config.storage) ? config.storage : {
131131
getItem: get,
@@ -160,7 +160,7 @@ function analytics(config = {}) {
160160
// throw new Error(`${ERROR_URL}3`)
161161
throw new Error('Abort disabled inListener')
162162
}
163-
163+
164164
// Parse URL parameters
165165
const params = paramsParse()
166166
// Initialize visitor information
@@ -177,8 +177,8 @@ function analytics(config = {}) {
177177
}
178178

179179
/**
180-
* Async Management methods for plugins.
181-
*
180+
* Async Management methods for plugins.
181+
*
182182
* This is also where [custom methods](https://bit.ly/329vFXy) are loaded into the instance.
183183
* @typedef {Object} Plugins
184184
* @property {EnablePlugin} enable - Set storage value
@@ -275,7 +275,7 @@ function analytics(config = {}) {
275275
// Merge in custom plugin methods
276276
...parsedOptions.methods
277277
}
278-
278+
279279
let readyCalled = false
280280
/**
281281
* Analytic instance returned from initialization
@@ -289,7 +289,7 @@ function analytics(config = {}) {
289289
* @property {On} on - Fire callback on analytics lifecycle events.
290290
* @property {Once} once - Fire callback on analytics lifecycle events once.
291291
* @property {GetState} getState - Get data about user, activity, or context.
292-
* @property {Storage} storage - storage methods
292+
* @property {AnalyticsStorage} storage - storage methods
293293
* @property {Plugins} plugins - plugin methods
294294
*/
295295
const instance = {
@@ -363,7 +363,7 @@ function analytics(config = {}) {
363363
* Track an analytics event. This will trigger `track` calls in any installed plugins
364364
* @typedef {Function} Track
365365
* @param {String} eventName - Event name
366-
* @param {Object} [payload] - Event payload
366+
* @param {TrackEventProperties} [payload] - Event payload
367367
* @param {Object} [options] - Event options
368368
* @param {Function} [callback] - Callback to fire after tracking completes
369369
* @returns {Promise}
@@ -733,7 +733,7 @@ function analytics(config = {}) {
733733
/**
734734
* Storage utilities for persisting data.
735735
* These methods will allow you to save data in localStorage, cookies, or to the window.
736-
* @typedef {Object} Storage
736+
* @typedef {Object} AnalyticsStorage
737737
* @property {GetItem} getItem - Get value from storage
738738
* @property {SetItem} setItem - Set storage value
739739
* @property {RemoveItem} removeItem - Remove storage value
@@ -887,7 +887,7 @@ function analytics(config = {}) {
887887
}
888888
return acc
889889
}, {})
890-
890+
891891
const initialState = {
892892
context: initialConfig,
893893
user: visitorInfo,
@@ -941,7 +941,7 @@ function analytics(config = {}) {
941941

942942
const enabledPlugins = pluginKeys.filter((name) => parsedOptions.pluginEnabled[name])
943943
const disabledPlugins = pluginKeys.filter((name) => !parsedOptions.pluginEnabled[name])
944-
944+
945945
/* Register analytic plugins */
946946
store.dispatch({
947947
type: EVENTS.registerPlugins,

packages/analytics-core/src/modules/page.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function currentUrl(search) {
3535
}
3636

3737
/**
38-
* Page data for overides
38+
* Page data for overrides
3939
* @typedef {object} PageData
4040
* @property {string} [title] - Page title
4141
* @property {string} [url] - Page url

packages/analytics-core/src/modules/track.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import EVENTS from '../events'
33
import serialize from '../utils/serialize'
44

5+
56
// Track State
67
const initialState = {
78
last: {},
+63-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,70 @@
1+
/**
2+
* @callback PluginTrackFunction
3+
* @param {Object} arg
4+
* @param {Object} arg.config config from the plugin spec
5+
* @param {AnalyticsInstance} arg.instance analytics instance
6+
* @param {Object} arg.payload event data
7+
* @param {string} arg.payload.event event name passed to track
8+
* @param {Object.<string,Object>} arg.payload.properties event properties passed to track
9+
* @return {void}
10+
*/
11+
12+
/**
13+
* @callback PluginPageFunction
14+
* @param {Object} arg
15+
* @param {Object} arg.config config from the plugin spec
16+
* @param {AnalyticsInstance} arg.instance analytics instance
17+
* @param {Object} arg.payload
18+
* @param {string} arg.payload.event
19+
* @param {PageData} arg.payload.properties
20+
* @return {void}
21+
*/
22+
23+
/**
24+
* @callback PluginIdentifyFunction
25+
* @param {Object} arg
26+
* @param {Object} arg.config config from the plugin spec
27+
* @param {AnalyticsInstance} arg.instance analytics instance
28+
* @param {Object} arg.payload
29+
* @param {string} arg.payload.userId
30+
* @param {Object.<string,Object>} arg.payload.traits
31+
* @return {void}
32+
*/
33+
34+
/**
35+
* @callback PluginInitializeFunction
36+
* @param {Object} arg
37+
* @param {Object} arg.config config from the plugin spec
38+
* @param {AnalyticsInstance} arg.instance analytics instance
39+
* @return boolean
40+
*/
41+
42+
/**
43+
* @callback PluginLoadedFunction
44+
* @param {Object} arg
45+
* @param {Object} arg.config config from the plugin spec
46+
* @param {AnalyticsInstance} arg.instance analytics instance
47+
* @param {Object} arg.payload
48+
* @return boolean
49+
*/
50+
51+
/**
52+
* @callback PluginReadyFunction
53+
* @param {Object} arg
54+
* @param {Object} arg.config config from the plugin spec
55+
* @param {AnalyticsInstance} arg.instance analytics instance
56+
* @return void
57+
*/
58+
159
/**
260
* @typedef {Object} AnalyticsPlugin
361
* @property {string} name - Name of plugin
462
* @property {Object} [EVENTS] - exposed events of plugin
563
* @property {Object} [config] - Configuration of plugin
664
* @property {function} [initialize] - Load analytics scripts method
7-
* @property {function} [page] - Page visit tracking method
8-
* @property {function} [track] - Custom event tracking method
9-
* @property {function} [identify] - User identify method
10-
* @property {function} [loaded] - Function to determine if analytics script loaded
11-
* @property {function} [ready] - Fire function when plugin ready
65+
* @property {PluginPageFunction} [page] - Page visit tracking method
66+
* @property {PluginTrackFunction} [track] - Custom event tracking method
67+
* @property {PluginIdentifyFunction} [identify] - User identify method
68+
* @property {PluginLoadedFunction} [loaded] - Function to determine if analytics script loaded
69+
* @property {PluginReadyFunction} [ready] - Fire function when plugin ready
1270
*/

0 commit comments

Comments
 (0)