rudderlabs / rudder-sdk-flutter

Flutter SDK for RudderStack - the Customer Data Platform for Developers. Now supports Android, iOS and Web tracking!
https://www.rudderstack.com
Other
6 stars 24 forks source link

SDK does not send device token on app open #32

Closed savy-91 closed 2 years ago

savy-91 commented 2 years ago

Hi!

I am setting up an integration with customer.io and therefore I'd like to push the device token there as well.

The issue I am having is that despite setting the device token correctly after initialization, the AppOpen event is not sent with the device token.

Here is an example of event sent by the Flutter SDK:

{
    "type": "track",
    "event": "Application Opened",
    "sentAt": "2021-11-17T15:42:38.444Z",
    "userId": "618d0ab3209c05224512062e",
    "channel": "mobile",
    "context": {
        "os": {
            "name": "Android",
            "version": "10"
        },
        "app": {
            "name": "XXX",
            "build": "1",
            "version": "0.0.2",
            "namespace": "XXX"
        },
        "device": {
            "id": "e1e5b4de53995195",
            "name": "generic_x86",
            "type": "Android",
            "model": "Android SDK built for x86",
            "manufacturer": "Google"
        },
        "locale": "it-IT",
        "screen": {
            "width": 1080,
            "height": 1794,
            "density": 420
        },
        "traits": {
            "id": "618d0ab3209c05224512062e",
            "userId": "618d0ab3209c05224512062e",
            "anonymousId": "e1e5b4de53995195"
        },
        "library": {
            "name": "com.rudderstack.android.sdk.core",
            "version": "1.0.22"
        },
        "network": {
            "wifi": true,
            "carrier": "Android",
            "cellular": true,
            "bluetooth": false
        },
        "timezone": "Europe/Rome",
        "userAgent": "Dalvik/2.1.0 (Linux; U; Android 10; Android SDK built for x86 Build/QSR1.190920.001)"
    },
    "rudderId": "5f8fe880-dc6b-4ea9-8837-5950292f309a",
    "messageId": "1637163746413-2747fe7b-adfa-4e50-8b92-4088588ba91d",
    "anonymousId": "e1e5b4de53995195",
    "integrations": {
        "All": true
    },
    "originalTimestamp": "2021-11-17T15:42:26.413Z"
}

This is the subsequent event that gets sent:

{
    "type": "screen",
    "event": "/",
    "sentAt": "2021-11-17T15:42:38.444Z",
    "userId": "618d0ab3209c05224512062e",
    "channel": "mobile",
    "context": {
        "os": {
            "name": "Android",
            "version": "10"
        },
        "app": {
            "name": "XXX",
            "build": "1",
            "version": "0.0.2",
            "namespace": "XXX"
        },
        "device": {
            "id": "e1e5b4de53995195",
            "name": "generic_x86",
            "type": "Android",
            "model": "Android SDK built for x86",
            "token": "XXX",
            "manufacturer": "Google"
        },
        "locale": "it-IT",
        "screen": {
            "width": 1080,
            "height": 1794,
            "density": 420
        },
        "traits": {
            "id": "618d0ab3209c05224512062e",
            "userId": "618d0ab3209c05224512062e",
            "anonymousId": "e1e5b4de53995195"
        },
        "library": {
            "name": "com.rudderstack.android.sdk.core",
            "version": "1.0.22"
        },
        "network": {
            "wifi": true,
            "carrier": "Android",
            "cellular": true,
            "bluetooth": false
        },
        "timezone": "Europe/Rome",
        "userAgent": "Dalvik/2.1.0 (Linux; U; Android 10; Android SDK built for x86 Build/QSR1.190920.001)"
    },
    "rudderId": "5f8fe880-dc6b-4ea9-8837-5950292f309a",
    "messageId": "1637163746936-7d5938d0-48fa-431e-933e-387d3711803d",
    "properties": {
        "name": "/"
    },
    "anonymousId": "e1e5b4de53995195",
    "integrations": {
        "All": true
    },
    "originalTimestamp": "2021-11-17T15:42:26.936Z"
}

It seems to me that the Application Open event is sent before the token is set.

The documentation states:

To use this feature, you've to turn on the feature trackApplicationLifecycleEvents in your mobile SDK implementation code. Moreover, you have to register your deviceToken after initializing the SDK.

Which is what I am doing.

The problem persists after subsequent app restarts, therefore I assume that the token is not persisted by the Rudder sdk.

The additional problem is that due to the way Rudderstack integrates with Customer.io, there is no way for me to register the fcm token at a later stage as:

We register the deviceToken to Customer.io on the following Application Lifecycle Events.

Application Installed Application Opened Application Unistalled

It would be great if you could have a look at this as I believe it could be a bug in the Rudderstack SDK for Flutter and as a temporary solution it could also work if I could manually send the device token to Customer.io in a separate event.

savy-91 commented 2 years ago

If in the future anybody else has this problem, this is the workaround I am using.

I created a transformation that, given an event setDeviceToken runs this function:

export async function setCustomerIoToken(event) {
    const customer_id = event.userId;
    const token = event.properties['$token'];
    const requestUrl = `https://track-eu.customer.io/api/v1/customers/${customer_id}/devices`;
    const os = 'android';
    const auth = 'AUTH_STRING'
    const payload = {
        "device": {
            "id": token,
            "platform": 'android'
        }
    };
    const res = await fetch(requestUrl, {
    method: "PUT",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Basic ${auth}`,
    },
    body: JSON.stringify(payload),
  });
  return res;
}
savy-91 commented 2 years ago

I see you guys are working on this in #33 :rocket:

pallabmaiti commented 2 years ago

Hi @savy-91 we already supported this annonymousId fix in this https://github.com/rudderlabs/rudder-sdk-flutter/pull/33