Migrate from Segment to RudderStack

Migrate from Segment to RudderStack while backfilling user data with minimal code changes.

This guide explains the step-by-step process of migrating from Segment to RudderStack and backfilling user data. It covers the necessary steps for replacing your instrumentation code and start using the RudderStack SDKs to track your events with minimal code changes.

Migrate the workspace

Sign up for RudderStack Cloud to see the RudderStack dashboard containing your data plane URL:

RudderStack data plane URL

Similar to Segment, you must set up your source-destination connections in the dashboard to facilitate the event data flow.

Contact the RudderStack team if you need help in managing your hosting.

Update SDK implementation

Depending on the platform, follow these steps to move your existing SDK implementation to RudderStack:

Migrate and backfill user data from Segment

When migrating from Segment or a similar analytics tool, you might have some anonymous traffic that has not yet been identified. When Segment and RudderStack track events for non-identified users, both assign a random UUID as an anonymousId. This ID is used to track an unknown user until they are identified. It also allows RudderStack to track user behavior, journeys, and first-touch attribution before and after the users are identified.

You can use the following options to migrate and backfill user data in RudderStack that was previously captured in Segment:

Using Segment SDK

If you have captured the anonymous IDs, user IDs, and traits with Segment, use this option to migrate all the data to RudderStack using the Segment SDK.

Benefits

  • Reliable as the supported APIs of RudderStack and Segment SDK are used.
  • Better code readability.
  • Access to all the user data by adding both SDKs.

Implementation details

  • Need to load the RudderStack and Segment SDK synchronously.
  • [Optional] Instead of overriding the RudderStack anonymous ID with Segment’s, you can add it as a trait instead (for example, segmentAnonymousId).

Steps

  1. Load the RudderStack JavaScript SDK along with the Segment JS SDK. Make sure the Segment SDK is loaded before the RudderStack SDK.
  2. [Optional] Put the RudderStack load script inside Segment’s ready callback.
  3. Add the following snippet in RudderStack’s onLoaded callback:
rudderanalytics.load(WRITE_KEY, DATA_PLANE_URL, {
    onLoaded: function(rudderanalytics) {
        const segmentAnon = analytics.user().anonymousId();
        const segmentUserId = analytics.user().id();
        // you can also retrieve user traits if you would like
        const rudderUserId = rudderanalytics.getUserId();
        if (segmentAnon) {
            rudderanalytics.setAnonymousId(segmentAnon)
        }
        if (segmentUserId && (!rudderUserId || (rudderUserId != segmentUserId))) {
            // you can change the logic above to whatever makes sense
            // i.e. you can choose to identify the user only if the 
            // rudderstack user id is not present and the segment one is
            rudderanalytics.identify(segmentUserId)
            // you can also add the segment traits in the second parameter
        }
    }
});
  1. Finally, change all your Segment event calls to RudderStack event calls.

Only anonymous IDs

Use this option when you want to maintain continuity in user tracking using the anonymousId. This is often the case when tracking anonymous users/traffic is a primary concern, and/or when other user IDs and traits will automatically be picked up by RudderStack through identify calls (that is, all other IDs and traits are passed in identify calls every time users log-in).

Benefits

  • Reliable and quick implementation with minimal code changes.
  • Does not require Segment SDK or synchronous loading.

Implementation details

  • Works only for anonymous IDs which means you won’t be able to migrate the user IDs, traits, etc. unless you implement some custom instrumentation.

Steps

Migrate anonymousId values automatically by capturing Segment’s anonymousId and setting it as RudderStack anonymousId. It uses the JavaScript SDK’s anonymousIdOptions object which, when enabled, reads the Segment anonymousId from your local storage (localStorage) or the Segment cookie and sets it accordingly.

For more information on how to enable the anonymousIdOptions object, see Capturing anonymousId automatically.

From local storage

Use this option if you have leveraged a local storage in your Segment implementation and need to retrieve the Segment IDs from there. After manually retrieving the Segment IDs stored in a user’s local storage, you can follow this approach to pass them in the RudderStack payloads. You can do this without loading the Segment SDK by using the browser’s local storage getItem call.

Benefits

  • Does not require the Segment SDK and you can control which values to migrate.
  • Negligible synchronous loading (onLoaded callback does not take up much resources).

Implementation details

  • Custom implementation using a local storage getItem call instead of Segment API to retrieve items.
  • Pull from cookies instead of local storage (using regex and more code).
  • Delete values from local storage/cookies after migration to clean up.

Steps

  1. Load the RudderStack JavaScript SDK.
  2. Add the following snippet in RudderStack’s onLoaded callback:
rudderanalytics.load(WRITE_KEY, DATA_PLANE_URL, {
    onLoaded: function(rudderanalytics) {
        const segmentAnon = localStorage.getItem("ajs_anonymous_id");
        const segmentUserId = localStorage.getItem("ajs_user_id");
        // you can also retrieve user traits if you would like
        const rudderUserId = rudderanalytics.getUserId();
        if (segmentAnon) {
            rudderanalytics.setAnonymousId(segmentAnon)
        }
        if (segmentUserId && (!rudderUserId || (rudderUserId != segmentUserId))) {
            // you can change the logic above to whatever makes sense
            // i.e. you can choose to identify the user only if the 
            // rudderstack user id is not present and the segment one is
            rudderanalytics.identify(segmentUserId)
            // you can also add the segment traits in the second parameter
        }
    }
});
  1. Finally, change all your Segment event calls to RudderStack event calls.

Questions? Contact us by email or on Slack