> For the complete documentation index, see [llms.txt](https://rudderlabs.gitbook.io/rudderlabs-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rudderlabs.gitbook.io/rudderlabs-1/docs/user-guides/migration-guides/rudderstack-migration-guide.md).

# Migrating from Segment to RudderStack

Step-by-step guide on migrating from Segment to RudderStack.

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

## Migrating the workspace

After creating a [RudderStack](https://app.rudderstack.com/signup?type=freetrial) account, you will see the following dashboard containing your data plane URL:

![RudderStack dashboard](https://876606571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lq5Ea6fHVg3dSxMCgyQ%2Fuploads%2Fgit-blob-aa136fe83a152211c0cc3cff9501fab29c328c73%2Frudderstack-dashboard.png?alt=media)

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

Keep your source write key and the data plane URL handy.

If you need more support or want us to manage your hosting, you can [contact us](mailto:%20support@rudderstack.com).

## Updating the SDK implementation

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

Android iOS JavaScript .NET React Native

* Change the dependencies in your `app/build.gradle` file add the following:

  ```groovy
  repositories {
      mavenCentral()
  }
  ```
* Under dependencies, add the following:

  ```groovy
  implementation 'com.rudderstack.android.sdk:core:1.0.1'
  implementation 'com.google.code.gson:gson:2.8.6'
  ```
* 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.

  ```java
  RudderClient rudderClient = RudderClient.getInstance(
          this,
          <WRITE_KEY>,
          new RudderConfig.Builder()
                  .withDataPlaneUrl(<DATA_PLANE_URL>)
                  .withLogLevel(RudderLogger.RudderLogLevel.DEBUG)
                  .withTrackLifecycleEvents(true)
                  .withRecordScreenViews(true)
                  .build()
  );
  ```
* Update the use of the classes according to the following table:

  | 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.

  * Change the dependencies in the `Podfile` of your project:

    ```ruby
    pod 'Rudder'
    ```
  * Update your SDK initialization to the following. Replace `WRITE_KEY` and `DATA_PLANE_URL` with your source write key and data plane URL obtained above.

    ```objectivec
    RudderConfigBuilder *builder = [[RudderConfigBuilder alloc]init];
    [builder withDataPlaneUrl:<DATA_PLANE_URL>];
    [RudderClient getInstance:<WRITE_KEY> config:[builder build]];
    ```

    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 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 without any changes as the RudderStack API is fully compatible with Segment.

    * Add the SDK to your web application:

      ```markup
      <script>
      	rudderanalytics = window.rudderanalytics = [];

      	var methods = [
      		"load",
      		"page",
      		"track",
      		"identify",
      		"reset"
      	];
      	for (var i=0; i<methods.length; i++) {
      		var method = methods[i];
      		rudderanalytics[method] = function(methodName) {
      			return function() {
      				rudderanalytics.push([methodName, ...arguments]);
      			}
      		} (method)
      	}
      	rudderanalytics.load(<WRITE_KEY>, <DATA_PLANE_URL>);
      	rudderanalytics.page();
      </script>
      <script src="https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js"></script>
      ```

      For the minified version of the above script, refer to the [RudderStack JavaScript SDK](https://www.rudderstack.com/docs/stream-sources/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk/quick-start-guide/) guide.

      If you are using an older version of the JavaScript SDK (v1), refer to the [Version Migration Guide](https://www.rudderstack.com/docs/stream-sources/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk/version-migration-guide/) to migrate to v1.1.
    * Update the object. RudderStack uses `rudderanalytics` as the global object library as compared to Segment's `analytics` object.

      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](https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-powershell)
    * Then, 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.

      ```csharp
      using RudderStack;

      RudderAnalytics.Initialize(
           <WRITE_KEY>,
           new RudderConfig(dataPlaneUrl: <DATA_PLANE_URL>)
      );
      ```

      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:

      ```bash
      // 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
      ```
    * Then, import the React Native SDK.

      ```typescript
      import rudderClient from "@rudderstack/rudder-sdk-react-native"
      ```
    * Next, 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.

      ```typescript
      await rudderClient.setup(<WRITE_KEY>, {
        dataPlaneUrl: <DATA_PLANE_URL>,
        trackLifecycleEvents: true,
        recordScreenViews: true,
      })
      ```
    * 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.

    ### Backfilling Segment anonymous IDs

    When migrating from Segment or a similar analytics tool, you might already 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.

    ### Capturing Segment `anonymousId` automatically

    You can automatically capture the Segment `anonymousId` and set it as Rudderstack `anonymousId` using the JavaScript SDK's `anonymousIdOptions` object. When enabled, it 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, refer to the [Capturing anonymousId automatically](https://www.rudderstack.com/docs/stream-sources/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk/#capturing-anonymousid-automatically) section.

    ### Contact us

    If you're stuck somewhere in the migration process, you can [contact us](mailto:%20support@rudderstack.com) or start a conversation in our [Slack](https://rudderstack.com/join-rudderstack-slack-community) community.
