DavidWells / analytics

Lightweight analytics abstraction layer for tracking page views, custom events, & identifying visitors
https://getanalytics.io
MIT License
2.46k stars 243 forks source link

The API for plugin enabling doesn't make sense #355

Closed matthew-dean closed 1 year ago

matthew-dean commented 1 year ago

The docs have code like this:

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    googleAnalytics({
      trackingId: 'ua-111-22222',
      // Disable GA from loading 
      enabled: false,
    }),
  ]
})

analytics.plugins.enable('google-analytics')

However, it's not clear how google-analytics is mapped to googleAnalytics? I would have guessed it's converting dash-case to camelCase, but then later it has this example:

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    googleAnalyticsPlugin({
      trackingId: 'UA-xyz-123',
      // Disable GA from loading until `analytics.plugins.enable` called 
      enabled: false,
    })
  ]
})
analytics.plugins.enable('google-analytics')

Okay, so then I thought maybe this was an internal name defined in the plugin? If that's the case, then how do you do this:

  analytics = Analytics({
    app: 'app',
    plugins: [
      googleTagManager({
        containerId: 'GTM-SOMETHING1',
        enabled: false
      })
      googleTagManager({
        containerId: 'GTM-SOMETHING2',
        enabled: false
      })
    ]
  })

  // Now I want to enable just the first Google Tag Manager bucket
  analytics.plugins.enable('???')
DavidWells commented 1 year ago

You can’t have duplicate plug-in name spaces. Plugins are objects if you log them out you can see the plug-in names. For the 2nd Instance you need to spread a new “name” onto the second plug-in object

DavidWells commented 1 year ago

You can see how to add multiple instances of a analytics provider here: https://github.com/DavidWells/analytics/issues/47#issuecomment-627127712

You just need to spread a new name key and thats how you enable/disable