From a65f2f1c745e7cf9db8a9f1520cfe8624e7ce66c Mon Sep 17 00:00:00 2001 From: Prayansh Srivastava Date: Thu, 18 Jun 2020 12:51:01 -0700 Subject: [PATCH] fix(config): pass user-supplied context for analytics calls --- packages/core/src/analytics.ts | 10 +++++----- packages/core/src/middleware.ts | 9 +++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index ff28fa832..acb303e65 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -213,7 +213,7 @@ export module Analytics { * @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to) */ public async track(event: string, properties: JsonMap = {}, options: Options = {}) { - await this.middlewares.run('track', { event, properties, integrations: options.integrations || {} }) + await this.middlewares.run('track', { event, properties, integrations: options.integrations || {} }, options.context || {}) } /** @@ -231,7 +231,7 @@ export module Analytics { * If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc. */ public async screen(name: string, properties: JsonMap = {}, options: Options = {}) { - await this.middlewares.run('screen', { name, properties, integrations: options.integrations || {} }) + await this.middlewares.run('screen', { name, properties, integrations: options.integrations || {} }, options.context || {}) } /** @@ -246,7 +246,7 @@ export module Analytics { * @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to) */ public async identify(user: string, traits: JsonMap = {}, options: Options = {}) { - await this.middlewares.run('identify', { user, traits, integrations: options.integrations || {} }) + await this.middlewares.run('identify', { user, traits, integrations: options.integrations || {} }, options.context || {}) } /** @@ -259,7 +259,7 @@ export module Analytics { * @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to) */ public async group(groupId: string, traits: JsonMap = {}, options: Options = {}) { - await this.middlewares.run('group', { groupId, traits, integrations: options.integrations || {} }) + await this.middlewares.run('group', { groupId, traits, integrations: options.integrations || {} }, options.context || {}) } /** @@ -272,7 +272,7 @@ export module Analytics { * The existing ID will be either the previousId if you have called identify, or the anonymous ID. */ public async alias(newId: string, options: Options = {}) { - await this.middlewares.run('alias', { newId, integrations: options.integrations || {} }) + await this.middlewares.run('alias', { newId, integrations: options.integrations || {} }, options.context || {}) } /** diff --git a/packages/core/src/middleware.ts b/packages/core/src/middleware.ts index a593c4f77..f89ed51c9 100644 --- a/packages/core/src/middleware.ts +++ b/packages/core/src/middleware.ts @@ -81,9 +81,11 @@ export class MiddlewareChain { public async run>( type: T, - data: P['data'] + data: P['data'], + context: JsonMap ) { const ctx: Context = { + ...context, library: { name: 'analytics-react-native', version: require('../package.json').version @@ -92,11 +94,6 @@ export class MiddlewareChain { const payload: Payload = await this.exec(type, ctx, data) - const opts: Options = { - context: payload.context, - integrations: payload.data.integrations - } - switch (payload.type) { case 'alias': return this.wrapper.run('alias', alias =>