Migrating from Segment to RudderStack
Step-by-step guide to migrating from Segment to RudderStack

Introduction

This document explains the step-by-step process of migrating from Segment to RudderStack. Our primary goal is to lay out the necessary actions for replacing your instrumentation code for generating the events from using the Segment SDK to RudderStack SDK with minimal changes.

Migrating the Workspace

Start with creating an account on the RudderStack dashboard. Similar to Segment, you will need to create sources and destinations in the dashboard. This will help you create the necessary connections for the event data to flow from your sources to the destination.
You can also check our guide on how to add sources and destinations in RudderStack.

Setting up the RudderStack Data Plane

RudderStack requires a data plane for the events to flow through. You can choose to set it up yourself within your cloud computing environment. Please check our installation guide to set up RudderStack.
We also offer a version of the data plane where we host it within our VPC. You can turn on the RudderStack Hosted Service button on the Connections page of your dashboard to get started with it. ‌
If you need more support or you want us to manage your hosting, please feel free to contact us.

Updating SDK implementation

Depending on the platform, please follow these steps to move your existing SDK implementation to RudderStack:
Android
iOS
JavaScript
    Change the dependencies in your app/build.gradle file add the following:
1
repositories {
2
mavenCentral()
3
}
Copied!
    Under dependencies, add the following:
1
implementation 'com.rudderstack.android.sdk:core:1.0.1'
2
implementation 'com.google.code.gson:gson:2.8.6'
Copied!
    Update your SDK initialization to the following:
1
RudderClient rudderClient = RudderClient.getInstance(
2
this,
3
<YOUR_WRITE_KEY>,
4
new RudderConfig.Builder()
5
.withDataPlaneUrl(<DATA_PLANE_URL>)
6
.withLogLevel(RudderLogger.RudderLogLevel.DEBUG)
7
.withTrackLifecycleEvents(true)
8
.withRecordScreenViews(true)
9
.build()
10
);
Copied!
    Update the use of the classes according to the table below:
Segment
RudderStack
Analytics
RudderClient
Traits
RudderTraits
Property
RudderProperty
You can use the rest of your code as is, as RudderStack SDK is API-compatible with Segment.
    Change the dependencies in the Podfile of your project:
1
pod 'Rudder'
Copied!
    Update your SDK initialization to the following:
1
RudderConfigBuilder *builder = [[RudderConfigBuilder alloc]init];
2
[builder withDataPlaneUrl:<DATA_PLANE_URL>];
3
[RudderClient getInstance:<WRITE_KEY> config:[builder build]];
Copied!
The instance of the RudderClient is available at [RudderClient sharedInstance] whereas Segment Instance is available at [Analytics sharedAnalytics]
    Update the imports from Analytics.h to Rudder.h wherever necessary
    Update the use of the classes according to the table below:
Segment
RudderStack
Analytics
RudderClient
Traits
RudderTraits
Property
RudderProperty
You can use the rest of your code as is, as RudderStack SDK is API compatible with Segment
    Add the SDK to your web application:
1
<script>
2
rudderanalytics = window.rudderanalytics = [];
3
4
var methods = [
5
"load",
6
"page",
7
"track",
8
"identify",
9
"reset"
10
];
11
for (var i=0; i<methods.length; i++) {
12
var method = methods[i];
13
rudderanalytics[method] = function(methodName) {
14
return function() {
15
rudderanalytics.push([methodName, ...arguments]);
16
}
17
} (method)
18
}
19
rudderanalytics.load(<YOUR_WRITE_KEY>, <DATA_PLANE_URI>);
20
rudderanalytics.page();
21
</script>
22
<script src="https://cdn.rudderlabs.com/rudder-analytics.min.js"></script>
Copied!
For the minified version of the above script please refer to the RudderStack JavaScript SDK guide.
    Update the object. We use rudderanalytics as the global object library in comparison to Segment's analytics object
You can use the rest of your code as is, as the RudderStack SDK is fully API-compatible with Segment

Backfilling Segment AnonymousIds

When migrating from Segment or a similar analytics tool, you likely already have some amount of anonymous traffic that has not yet been identified. When Segment or 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 and allows us to stitch together user behavior, journeys, and first touch attribution before and after they are identified.
To avoid duplicating these previously assigned anonymous users, we recommend loading the RudderStack SDK in the ready callback of the segment SDK for a period of time. By loading RudderStack in the callback, we can retrieve the previously assigned anonymousId from the segment cookie and assign that same anonymousId to the RudderStack user while initializing the RudderStack SDK. After we have overlapped the SDKs enough to feel confident that the majority of our anonymousId have been backfilled, we can remove the Segment SDK and begin using only the RudderStack SDK.
A code snippet for loading the SDKs in parallel is shown below:
1
<script type="text/javascript">
2
!function(){var e=window.rudderanalytics=window.rudderanalytics||[];e.methods=["load","page","track","identify","alias","group","ready","reset","getAnonymousId","setAnonymousId"],e.factory=function(t){return function(){var r=Array.prototype.slice.call(arguments);return r.unshift(t),e.push(r),e}};for(var t=0;t<e.methods.length;t++){var r=e.methods[t];e[r]=e.factory(r)}e.loadJS=function(e,t){var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.rudderlabs.com/v1/rudder-analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a)}}()
3
!(function(){
4
// Create a queue, but don't obliterate an existing one!
5
var analytics = window.analytics = window.analytics || [];
6
// If the real analytics.js is already on the page return.
7
if (analytics.initialize) return;
8
// If the snippet was invoked already show an error.
9
if (analytics.invoked) {
10
if (window.console && console.error) {
11
console.error('Segment snippet included twice.');
12
}
13
return;
14
}
15
// Invoked flag, to make sure the snippet
16
// is never invoked twice.
17
analytics.invoked = true;
18
// A list of the methods in Analytics.js to stub.
19
analytics.methods = [
20
'trackSubmit',
21
'trackClick',
22
'trackLink',
23
'trackForm',
24
'pageview',
25
'identify',
26
'reset',
27
'group',
28
'track',
29
'ready',
30
'alias',
31
'debug',
32
'page',
33
'once',
34
'off',
35
'on',
36
'addSourceMiddleware',
37
'addIntegrationMiddleware',
38
'setAnonymousId',
39
'addDestinationMiddleware'
40
];
41
// Define a factory to create stubs. These are placeholders
42
// for methods in Analytics.js so that you never have to wait
43
// for it to load to actually record data. The `method` is
44
// stored as the first argument, so we can replay the data.
45
analytics.factory = function(method){
46
return function(){
47
var args = Array.prototype.slice.call(arguments);
48
args.unshift(method);
49
analytics.push(args);
50
return analytics;
51
};
52
};
53
// For each of our methods, generate a queueing stub.
54
for (var i = 0; i < analytics.methods.length; i++) {
55
var key = analytics.methods[i];
56
analytics[key] = analytics.factory(key);
57
}
58
// Define a method to load Analytics.js from our CDN,
59
// and that will be sure to only ever load it once.
60
analytics.load = function(key, options){
61
// Create an async script element based on your key.
62
var script = document.createElement('script');
63
script.type = 'text/javascript';
64
script.async = true;
65
script.src = 'https://cdn.segment.com/analytics.js/v1/'
66
+ key + '/analytics.min.js';
67
// Insert our script next to the first script element.
68
var first = document.getElementsByTagName('script')[0];
69
first.parentNode.insertBefore(script, first);
70
analytics._loadOptions = options;
71
};
72
// Add a version to keep track of what's in the wild.
73
analytics.SNIPPET_VERSION = '4.1.0';
74
// Load Analytics.js with your key, which will automatically
75
// load the tools you've enabled for your account. Boosh!
76
analytics.load("SEGMENT_WRITE_KEY");
77
// Make the first page call to load the integrations. If
78
// you'd like to manually name or tag the page, edit or
79
// move this call however you'd like.
80
analytics.page();
81
// analytics ready callback
82
analytics.ready(function() {
83
// INITIALIZE RUDDER SDK with setAnonymousId
84
window.rudderanalytics.unshift(["setAnonymousId", window.analytics.user().anonymousId()])
85
window.rudderanalytics.unshift(["load", "RUDDERSTACK_WRITE_KEY", "RUDDERSTACK_DATAPLANE_URL"])
86
window.rudderanalytics.page()
87
window.rudderanalytics.loadJS()
88
})})();
89
</script>
Copied!
Note that you will need to enter your SEGMENT_WRITE_KEY, RUDDERSTACK_WRITE_KEY, and RUDDERSTACK_DATAPLANE_URL in the above example.

Contact Us

Stuck somewhere in the migration process? Feel free to contact us. You can also talk to us on our Slack channel.
Last modified 26d ago