Analytics for Node.js Migration Guide



This version of Analytics for Node.js is in beta and Segment is actively working on this feature. Segment’s First-Access and Beta terms govern this feature.

If you’re using the classic version of Analytics Node.js, follow these steps to upgrade to the latest version of Analytics Node.js.

  1. Change the named imports.


    Before:

     import Analytics from 'analytics-node'
    

    After:

     import { Analytics } from '@segment/analytics-node'
    
  2. Change instantiation to have an object as the first argument.


    Before:

     var analytics = new Analytics('YOUR_WRITE_KEY');
    

    After:

     const analytics = new Analytics({ writeKey: '<YOUR_WRITE_KEY>' })
    
  3. Change flushing to graceful shutdown.


    Before:

     await analytics.flush(function(err, batch) {
         console.log('Flushed, and now this program can exit!');
     });
    

    After:

     await analytics.closeAndFlush()
    

Key differences between the classic and updated version

  • The callback call signature changed.


    Before:

      (err, batch) => void
    

    After:

      (err, ctx) => void
    
  • The flushAt configuration option changed to maxEventsInBatch.

  • The enable setting (for disabling analytics during tests) changed to disable. enable: false changed to disable: true.

Removals

The updated Analytics Node.js removed these configuration options:

The updated Analytics Node.js library removed undocumented behavior around track properties

Before:

analytics.track({
 ...
 event: 'Ultimate Played',
 myProp: 'abc'
})

After:

analytics.track({
 ...
 event: 'Ultimate Played',
 properties:  {
   myProp: 'abc'
 }
})

This page was last modified: 05 Apr 2023



Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
or
Create free account