Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix(config): pass user-supplied context for analytics calls (segmenti…
Browse files Browse the repository at this point in the history
  • Loading branch information
prayansh authored Jun 18, 2020
1 parent d236e41 commit 7429567
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {})
}

/**
Expand All @@ -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 || {})
}

/**
Expand All @@ -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 || {})
}

/**
Expand All @@ -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 || {})
}

/**
Expand All @@ -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 || {})
}

/**
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ export class MiddlewareChain {

public async run<T extends Payload['type'], P extends PayloadFromType<T>>(
type: T,
data: P['data']
data: P['data'],
context: JsonMap
) {
const ctx: Context = {
...context,
library: {
name: 'analytics-react-native',
version: require('../package.json').version
Expand All @@ -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 =>
Expand Down

0 comments on commit 7429567

Please sign in to comment.