analytics icon indicating copy to clipboard operation
analytics copied to clipboard

Adjust event name in enrichment plugin

Open oddnavy opened this issue 2 years ago • 2 comments

Is it possible to change/format an event name within a plugin for a specific namespace?

For example, for Google Analytics 4, I need to use specific event names and properties for the e-commerce data but don't necessarily want to use the Google Analytics event names everywhere.

I was hoping something like this would work, but the event gets swallowed:

// enricher plugin
const googleAnalyticsEnricherPlugin = {
  name: 'enrich-google-analytics',
  'track:google-analytics': ({ payload }) => {
    switch (payload.event) {
      case 'Product Clicked':
        return {
          ...payload,
          event: 'item_clicked',
          properties: {
            items: [
              {
                item_id: payload.properties.id,
                item_name: payload.properties.name,
              },
            ],
          },
        };

      default:
        return { ...payload };
    }
  },
};

// track call
analytics.track('Product Clicked', {
  id: product.id,
  name: product.name,
});

Adjusting the properties works fine but not the event name.

oddnavy avatar Jan 19 '23 06:01 oddnavy

I am seeing similar behavior in the google-tag-manager plugin. #363

ccook10-chwy avatar Feb 18 '23 17:02 ccook10-chwy

This is interesting one. Changing event isn't setup right now. Would be a good future improvement tho

Right now you can send provider specific events like this:

/* Send track call to one 1 place */
analytics.track('item_clicked', {
  price: 11,
  sku: '1234'
}, {
  plugins: {
    // disable this specific track call for all plugins except google-analytics
    all: false,
    ['google-analytics']: true
  }
})

DavidWells avatar May 27 '23 21:05 DavidWells