rudderlabs / rudder-sdk-react-native

Repository for RudderStack's React Native SDK - to track your event data via RudderStack.
https://www.rudderstack.com
Other
17 stars 18 forks source link

track call no way to add to context top level. #356

Closed DeanGracey closed 4 months ago

DeanGracey commented 4 months ago

Hello,

I'm trying to add data to the top level of the context object.

I can track an event like this:

rudderClient.track(
    event,
    {
      email,
      environment: ENV, //1
      userId,
      ...properties,
    },
    {
      additional: {
        environment: ENV, //2
      },
      env: ENV, //3
      something: 'else', //4
    },
  )

I get my env var within the properties object from 1. I can get it in additional under context from 2. I cannot get 3 or 4 as top level in the context object.

resulting JSON:

{
    "anonymousId": "6652f7fa-67b2-45d7-93b8-5d5663a9d608",
    "channel": "mobile",
    "context": {
        "additional": {
            "environment": "dev"
        },
        "app": {
            "build": "1",
            "name": "WeR1Dev",
            "namespace": "ai.wer1.dev.music.player",
            "version": "1.28.10"
        },
        "device": {
            "attTrackingStatus": 0,
            "id": "efabe9ac-2ef3-4e6b-b954-90e899cd7e62",
            "manufacturer": "Apple",
            "model": "arm64",
            "name": "iPhone SE (3rd generation)",
            "type": "iOS"
        },
        "library": {
            "name": "rudder-ios-library",
            "version": "1.27.0"
        },
        "locale": "en-ZA",
        "network": {
            "cellular": false,
            "wifi": true
        },
        "os": {
            "name": "iOS",
            "version": "17.2"
        },
        "screen": {
            "density": 2,
            "height": 667,
            "width": 375
        },
        "timezone": "Africa/Johannesburg",
        "traits": {
            "anonymousId": "6652f7fa-67b2-45d7-93b8-5d5663a9d608"
        }
    },
    "event": "player.set.paused",
    "integrations": {
        "All": true
    },
    "messageId": "3c25edaf-f087-410f-8e1d-791c792c4272",
    "originalTimestamp": "2024-06-24T08:57:45.331Z",
    "properties": {
        "email": "dean@wer1.ai",
        "environment": "dev",
        "set": {
            "artist": "Genesis, Bradley”,
            "artists": [
                {
                    "id": "a249ed40-74cd-4b4e-b3ac-f9ae345fbb4c",
                    "name": "Bradley”
                }
            ],
            "artwork": “*****”,
            "audioFile": "20240529093625_4291cd0a1b9647e6a3a89f3ddec956d9",
            "duration": 61592,
            "headers": {
            },
            "id": "b6cff169-169f-4a75-bcaf-4ed0c76ae69a",
            "ratingCount": 4,
            "ratingType": "like",
            "size": 949.9,
            "title": "Audio Collection",
            "type": "hls",
            "url": “****”
        },
        "source": "minified",
        "userId": “****”
    },
    "receivedAt": "2024-06-24T08:57:53.728Z",
    "request_ip": "102.132.174.178",
    "rudderId": "2f9932fd-65df-472e-be52-33421f37cf1f",
    "sentAt": "2024-06-24T08:57:53.266Z",
    "type": "track"
}

Is it possible to add to the top level context object?

1abhishekpandey commented 4 months ago

Hey @DeanGracey, To add custom context, you need to set the values in the following format:

const options = {
  tier: {
    category: 'premium',
    type: 'gold',
  },
};

Please follow our documentation on setting custom context for more details: https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-react-native-sdk/#setting-custom-context.

DeanGracey commented 4 months ago

hi @1abhishekpandey, That doesn't seem to work for a value (not object) at the top level.

i.e if I do the call like this:

  const options = {
    environment: ENV,
  }

  return rudderClient.track(
    event,
    {
      email,
      userId,
      ...properties,
    },
    options,
  )

I get this:

  "context": {
    "app": {
        "build": "1",
        "name": "WeR1Dev",
        "namespace": "ai.wer1.dev.music.player",
        "version": "1.28.10"
    },
    "device": {
        "attTrackingStatus": 0,
        "id": "efabe9ac-2ef3-4e6b-b954-90e899cd7e62",
        "manufacturer": "Apple",
        "model": "arm64",
        "name": "iPhone SE (3rd generation)",
        "type": "iOS"
    },
    "library": {
        "name": "rudder-ios-library",
        "version": "1.27.0"
    },
    "locale": "en-ZA",
    "network": {
        "cellular": false,
        "wifi": true
    },
    "os": {
        "name": "iOS",
        "version": "17.2"
    },
    "screen": {
        "density": 2,
        "height": 667,
        "width": 375
    },
    "timezone": "Africa/Johannesburg",
    "traits": {
        "anonymousId": "6652f7fa-67b2-45d7-93b8-5d5663a9d608"
    }
},
1abhishekpandey commented 4 months ago

Right, that's in our backlog, and we will support that in the future. For now, please send it like this:

const options = {
  environment: {
    ENV: true,
  }
};

You can also utilise our transformation feature to achieve your use case: https://www.rudderstack.com/docs/transformations/overview/.

DeanGracey commented 4 months ago

I see, thank you