Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine JSDoc / TypeScript types for plugins #324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions packages/analytics-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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 @@ -159,7 +159,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 @@ -176,8 +176,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 @@ -274,7 +274,7 @@ function analytics(config = {}) {
// Merge in custom plugin methods
...parsedOptions.methods
}

let readyCalled = false
/**
* Analytic instance returned from initialization
Expand All @@ -288,7 +288,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
Copy link
Contributor Author

@dobesv dobesv Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicted with the built-in type Storage used for localStorage & sessionStorage so I took the liberty of renaming it while I was in here to fix the TypeScript error I got. Hopefully that's OK.

* @property {Plugins} plugins - plugin methods
*/
const instance = {
Expand Down Expand Up @@ -732,7 +732,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 @@ -886,7 +886,7 @@ function analytics(config = {}) {
}
return acc
}, {})

const initialState = {
context: initialConfig,
user: visitorInfo,
Expand Down Expand Up @@ -940,7 +940,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
1 change: 1 addition & 0 deletions packages/analytics-core/src/modules/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import EVENTS from '../events'
import serialize from '../utils/serialize'


// Track State
const initialState = {
last: {},
Expand Down
68 changes: 63 additions & 5 deletions packages/analytics-core/src/pluginTypeDef.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add meta, anonymousId and userId as well? I haven't entirely figured which are present in what methods always.

https://getanalytics.io/resources/faq/#do-i-need-to-use-a-plugin

Copy link
Contributor Author

@dobesv dobesv Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, maybe? Doesn't seem like this PR is getting any attention from the library maintainer so I'm not sure making any changes to it makes practical sense.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it seems like issues and PRs are increasing. I'm sure @DavidWells is just busy at the moment. Maybe it's time for a call for maintainers? 🤔

Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
/**
* @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 {Object.<string,Object>} 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.userId
* @param {Object.<string,Object>} arg.payload.traits
* @return {void}
*/

/**
* @callback PluginInitializeFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @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
Comment on lines +65 to +69
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be one of these for every event named in events.js ?

*/