Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding segment group functionality to analytics npm package. See the docs https://getanalytics.io/plugins/segment/#adding-group-functionality
import Analytics from 'analytics'
import segmentPlugin from '@analytics/segment'
const originalSegmentInstance = segmentPlugin({
writeKey: '123-xyz'
})
// Extend originalSegmentInstance with custom methods
const enchancedSegmentInstance = Object.assign({}, originalSegmentInstance, {
methods: {
// add analytics.group method https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#group
group(groupId, traits = {}, options = {}, callback) {
const analyticsInstance = this.instance
// If no segment, return early
if (typeof window.analytics === 'undefined') {
return
}
// Make group call to segment
window.analytics.group(groupId, traits, options, callback)
},
}
}
// Initialize analytics instance with plugins
const analytics = Analytics({
app: 'your-app-name',
plugins: [
enchancedSegmentInstance,
]
})
// Usage:
// Now you can call segment.group in your app like so
analytics.plugins.segment.group('one', 'two', 'three')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment