Skip to content

Commit 6d7a0c6

Browse files
Merge pull request #4861 from segmentio/bcaudillo-patch-5
Added a portion to FAQ to include modifying context object
2 parents b31742d + 32e4757 commit 6d7a0c6

File tree

1 file changed

+29
-0
lines changed
  • src/connections/sources/catalog/libraries/mobile/react-native

1 file changed

+29
-0
lines changed

src/connections/sources/catalog/libraries/mobile/react-native/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,5 +692,34 @@ import {
692692
}
693693
}
694694
```
695+
### How do I add to the context Object?
696+
Like with the integrations object above, you'll need to use a plugin to access and modify the context object. For example, if you'd like to add `context.groupId` to every Track call, you should create a plugin like this:
697+
```js
698+
//in AddToContextPlugin.js
699+
import {
700+
Plugin,
701+
PluginType,
702+
SegmentEvent,
703+
} from '@segment/analytics-react-native';
704+
705+
export class AddToContextPlugin extends Plugin {
706+
// Note that `type` is set as a class property
707+
// If you do not set a type your plugin will be a `utility` plugin (see Plugin Types above)
708+
type = PluginType.enrichment;
709+
710+
async execute(event: SegmentEvent) {
711+
if (event.type == EventType.TrackEvent) {
712+
event.context['groupId'] = 'test - 6/8'
713+
}
714+
return event;
715+
}
716+
}
717+
// in App.js
718+
719+
import { AddToContextPlugin } from './AddToContextPlugin'
720+
721+
segmentClient.add({ plugin: new AddToContextPlugin() });
722+
```
723+
695724
## Changelog
696725
[View the Analytics React Native changelog on GitHub](https://github.com/segmentio/analytics-react-native/releases){:target="_blank"}.

0 commit comments

Comments
 (0)