Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Analytics for Node.js Migration Guide


If you're using the classic version of Analytics Node.js (named analytics-node on npm), upgrade to the latest version of Analytics Node.js (named @segment/analytics-node on npm).

  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:

    1
    await analytics.flush((err, batch) => {
    2
    console.log('Flushed, and now this program can exit!');
    3
    });

    After:

    await analytics.flush({ close: true })

Key differences between the classic and updated version

key-differences-between-the-classic-and-updated-version page anchor
  • The callback call signature changed.


    Before:

    (err, batch) => void

    After:

    (err, ctx) => void
  • The enable setting (for disabling analytics during tests) changed to disable. enable: false changed to disable: true.

Removals

removals page anchor

The updated Analytics Node.js removed these configuration options:

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

Before:

1
analytics.track({
2
...
3
event: 'Ultimate Played',
4
myProp: 'abc'
5
})

After:

1
analytics.track({
2
...
3
event: 'Ultimate Played',
4
properties: {
5
myProp: 'abc'
6
}
7
})