analytics icon indicating copy to clipboard operation
analytics copied to clipboard

analytics-plugin-segment missing category fields

Open vincentywx opened this issue 5 years ago • 3 comments

Hi @DavidWells, My team is using analytics-plugin-segment to send event to segment, and we noticed the segment can't receive category from the payload.

I debug in the plugin and found the code not handling the category fields.

I raised a PR already, could you please have a look. thanks

vincentywx avatar Sep 12 '21 07:09 vincentywx

Added comment to PR https://github.com/DavidWells/analytics/pull/212

Can you verify the change isn't breaking? You can extend the base plugin and try out your new method.

import Analytics from 'analytics'
import segmentPlugin from '@analytics/segment'

const baseSegmentPlugin = segmentPlugin({
  writeKey: '123-xyz'
})

const customSegmentPlugin = {
  ...baseSegmentPlugin,
  ...{
    // Your custom page overide function
  page: ({ payload, config }) => {
      if (typeof analytics === 'undefined') return
      const properties = payload.properties || {}
      const name = properties.name || properties.title
      const category = properties.category
      
      analytics.page(category, name, properties, {
        integrations: config.integrations,
        ...payload.options,
      })
    },
  }
}

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    customSegmentPlugin
  ]
})

analytics.page()

Side note: You can override any plugin method like this and can use your new setup without waiting for PR to merge in

DavidWells avatar Sep 12 '21 18:09 DavidWells

That's really handy to know how simple it is to override the methods!

Penners avatar Sep 13 '21 15:09 Penners

Released with @analytics/[email protected]

DavidWells avatar Sep 14 '21 04:09 DavidWells