Skip to content

Commit 7429567

Browse files
authored
fix(config): pass user-supplied context for analytics calls (#188)
1 parent d236e41 commit 7429567

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

packages/core/src/analytics.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export module Analytics {
213213
* @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to)
214214
*/
215215
public async track(event: string, properties: JsonMap = {}, options: Options = {}) {
216-
await this.middlewares.run('track', { event, properties, integrations: options.integrations || {} })
216+
await this.middlewares.run('track', { event, properties, integrations: options.integrations || {} }, options.context || {})
217217
}
218218

219219
/**
@@ -231,7 +231,7 @@ export module Analytics {
231231
* If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc.
232232
*/
233233
public async screen(name: string, properties: JsonMap = {}, options: Options = {}) {
234-
await this.middlewares.run('screen', { name, properties, integrations: options.integrations || {} })
234+
await this.middlewares.run('screen', { name, properties, integrations: options.integrations || {} }, options.context || {})
235235
}
236236

237237
/**
@@ -246,7 +246,7 @@ export module Analytics {
246246
* @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to)
247247
*/
248248
public async identify(user: string, traits: JsonMap = {}, options: Options = {}) {
249-
await this.middlewares.run('identify', { user, traits, integrations: options.integrations || {} })
249+
await this.middlewares.run('identify', { user, traits, integrations: options.integrations || {} }, options.context || {})
250250
}
251251

252252
/**
@@ -259,7 +259,7 @@ export module Analytics {
259259
* @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to)
260260
*/
261261
public async group(groupId: string, traits: JsonMap = {}, options: Options = {}) {
262-
await this.middlewares.run('group', { groupId, traits, integrations: options.integrations || {} })
262+
await this.middlewares.run('group', { groupId, traits, integrations: options.integrations || {} }, options.context || {})
263263
}
264264

265265
/**
@@ -272,7 +272,7 @@ export module Analytics {
272272
* The existing ID will be either the previousId if you have called identify, or the anonymous ID.
273273
*/
274274
public async alias(newId: string, options: Options = {}) {
275-
await this.middlewares.run('alias', { newId, integrations: options.integrations || {} })
275+
await this.middlewares.run('alias', { newId, integrations: options.integrations || {} }, options.context || {})
276276
}
277277

278278
/**

packages/core/src/middleware.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ export class MiddlewareChain {
8181

8282
public async run<T extends Payload['type'], P extends PayloadFromType<T>>(
8383
type: T,
84-
data: P['data']
84+
data: P['data'],
85+
context: JsonMap
8586
) {
8687
const ctx: Context = {
88+
...context,
8789
library: {
8890
name: 'analytics-react-native',
8991
version: require('../package.json').version
@@ -92,11 +94,6 @@ export class MiddlewareChain {
9294

9395
const payload: Payload = await this.exec(type, ctx, data)
9496

95-
const opts: Options = {
96-
context: payload.context,
97-
integrations: payload.data.integrations
98-
}
99-
10097
switch (payload.type) {
10198
case 'alias':
10299
return this.wrapper.run('alias', alias =>

0 commit comments

Comments
 (0)