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

feat: custom ID resolvers #230

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
47 changes: 24 additions & 23 deletions packages/analytics-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ import './pluginTypeDef'
function analytics(config = {}) {
const customReducers = config.reducers || {}
const initialUser = config.initialUser || {}
// @TODO add custom value reolvers for userId and anonId
// const resolvers = config.resolvers || {}

// if (BROWSER) {
// console.log('INIT browser')
// }

// 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 +123,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 All @@ -135,7 +133,12 @@ function analytics(config = {}) {

const getUserProp = getUserPropFunc(storage)

// mutable intregrations object for dynamic loading
const resolvers = config.resolvers || {
getUserId: (instance, payload) => getUserProp(ID, instance, payload),
getAnonId: (instance, payload) => getUserProp(ANONID, instance, payload)
}

// mutable integrations object for dynamic loading
let customPlugins = parsedOptions.plugins

/* Grab all registered events from plugins loaded */
Expand Down Expand Up @@ -177,8 +180,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 @@ -345,7 +348,7 @@ function analytics(config = {}) {
/* sets temporary in memory id. Not to be relied on */
set(tempKey(ID), id)

const resolvedId = id || data.userId || getUserProp(ID, instance, data)
const resolvedId = id || data.userId || resolvers.getUserId(instance, data)

return new Promise((resolve) => {
store.dispatch({
Expand Down Expand Up @@ -415,14 +418,17 @@ function analytics(config = {}) {
const data = isObject(eventName) ? eventName : (payload || {})
const opts = isObject(options) ? options : {}

const userId = resolvers.getUserId(instance, data)
const anonymousId = resolvers.getAnonId(instance, data)

return new Promise((resolve) => {
store.dispatch({
type: EVENTS.trackStart,
event: name,
properties: data,
options: opts,
userId: getUserProp(ID, instance, payload),
anonymousId: getUserProp(ANONID, instance, payload),
userId,
anonymousId,
}, resolve, [payload, options, callback])
})
},
Expand Down Expand Up @@ -471,21 +477,16 @@ function analytics(config = {}) {
const d = isObject(data) ? data : {}
const opts = isObject(options) ? options : {}

/*
// @TODO add custom value reolvers for userId and anonId
if (resolvers.getUserId) {
const asyncUserId = await resolvers.getUserId()
console.log('x', x)
}
*/
const userId = resolvers.getUserId(instance, d)
const anonymousId = resolvers.getAnonId(instance, d)

return new Promise((resolve) => {
store.dispatch({
type: EVENTS.pageStart,
properties: getPageData(d),
options: opts,
userId: getUserProp(ID, instance, d),
anonymousId: getUserProp(ANONID, instance, d),
userId,
anonymousId,
}, resolve, [data, options, callback])
})
},
Expand All @@ -508,10 +509,10 @@ function analytics(config = {}) {
*/
user: (key) => {
if (key === ID || key === 'id') {
return getUserProp(ID, instance)
return resolvers.getUserId(instance, null)
}
if (key === ANONID || key === 'anonId') {
return getUserProp(ANONID, instance)
return resolvers.getAnonId(instance, null)
}
const user = instance.getState('user')
if (!key) return user
Expand Down Expand Up @@ -887,7 +888,7 @@ function analytics(config = {}) {
}
return acc
}, {})

const initialState = {
context: initialConfig,
user: visitorInfo,
Expand Down Expand Up @@ -941,7 +942,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