Migrate from Segment to RudderStack while backfilling user data with minimal code changes.
7 minute read
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.
Update your SDK initialization to the following snippet. Replace WRITE_KEY and DATA_PLANE_URL with your source write key and data plane URL obtained above.
The instance of the RudderClient is available at [RudderClient sharedInstance] whereas the Segment instance is available at [Analytics sharedAnalytics]
Update the imports from Analytics.h to Rudder.h wherever necessary.
Update the class names as follows:
Segment
RudderStack
Analytics
RudderClient
Traits
RudderTraits
Property
RudderProperty
You can use the rest of your code without any changes as the RudderStack API is fully compatible with Segment.
For the minified version of the above script, see the JavaScript SDK documentation.
If you are using an older version of the JavaScript SDK (v1 or v1.1), see the migration guide to migrate to v3.
Update the Segment’s analytics object to RudderStack’s rudderanalytics.
You can use the rest of your code without any changes as the RudderStack API is fully compatible with Segment.
Change your .NET project’s dependencies to include the RudderAnalytics package via NuGet.
Initialize the RudderStack client by running the following code snippet. Replace WRITE_KEY and DATA_PLANE_URL with your source write key and data plane URL obtained above.
You can use the rest of your code without any changes as the RudderStack API is fully compatible with Segment.
Install the RudderStack React Native SDK by running the following command:
// npm
npm install @rudderstack/rudder-sdk-react-native --save
// yarn
yarn add @rudderstack/rudder-sdk-react-native
// navigate to iOS folder and run pod install
pod install
Change the SDK initialization as shown in the following snippet. Replace WRITE_KEY and DATA_PLANE_URL with your source write key and data plane URL obtained above.
Update the references to the analytics keyword in your application code with rudderClient.
You can use the rest of your code without any changes as the RudderStack API is fully compatible with Segment.
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
Load the RudderStack JavaScript SDK along with the Segment JS SDK. Make sure the Segment SDK is loaded before the RudderStack SDK.
Add the following snippet in RudderStack’s onLoaded callback:
rudderanalytics.load(WRITE_KEY,DATA_PLANE_URL,{onLoaded:function(rudderanalytics){constsegmentAnon=analytics.user().anonymousId();constsegmentUserId=analytics.user().id();// you can also retrieve user traits if you would like
construdderUserId=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
}}});
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.
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.
Add the following snippet in RudderStack’s onLoaded callback:
rudderanalytics.load(WRITE_KEY,DATA_PLANE_URL,{onLoaded:function(rudderanalytics){constsegmentAnon=localStorage.getItem("ajs_anonymous_id");constsegmentUserId=localStorage.getItem("ajs_user_id");// you can also retrieve user traits if you would like
construdderUserId=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
}}});
Finally, change all your Segment event calls to RudderStack event calls.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.