aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Unable to create a dynamic segment using identifyUser #14024

Open renso3x opened 3 days ago

renso3x commented 3 days ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Analytics

Amplify Version

v6

Amplify Categories

analytics

Backend

None

Environment information

``` ```

Describe the bug

I was creating a dynamic segment in AWS Pinpoint to filter users based on the organizationName. Initially, I encountered an issue where supplying only customProperties resulted in no endpoints being filtered. Additionally, it appears that triggering an event is required to apply the segment criteria.

Upon further investigation, I found that including options.userAttributes allows segments to be filtered based on this attribute. However, the documentation provided here is unclear about how to create segments effectively. Furthermore, version 6 of the library seems to have limited features for this purpose.

Expected behavior

I expect that when I trigger the identifyUser I can create a Segment based on those user criteria

Reproduction steps

npm install aws-amplify @aws-amplify/analytics

Code Snippet

// Put your code below this line.

await identifyUser({
    userId,
    userProfile: {
      customProperties: {
        organizationName: 'ABC',
      },
    },
    options: {
      userAttributes: {
        organizationName: 'ABC',
      },
    },
  });

record({
  event: EventTrackerType.USER_SIGNIN,
  eventData: { email: userId },
});

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 2 days ago

Hello, @renso3x 👋 and thanks for opening this issue. Pinpoint's updateEndpoint API is called under the hood when identifyUser() is called by Amplify, so we'll look into this further to determine if there's a bug in how the attributes are being passed. Can you confirm if you see the organizationName attribute being updated properly when you make the identifyUser() call?

If you create a Segment in Pinpoint console (see here), are you trying to then use this attribute to add user to an existing one? Or is the goal to have a new segment made when the identifyUser() API is called from the client side?

renso3x commented 2 days ago

Hi @cwomack,

Yes I am seeing the organizationName after an hour or so. I’m working on creating a segment using identifyUser(). Should I only use identifyUser() and skip calling record()?

In v5, it was necessary to call both identifyUser() with updated user attributes and record() for events. Could you confirm if this step is still required, or does identifyUser() alone suffice for updating the segment in the current implementation?

cwomack commented 1 day ago

@renso3x, thanks for the additional context here. That delay you see for the organizationName being around 45 min to an hour is expected behavior within the Pinpoint console (and a subject we can't really control on the Amplify side). Same thing happens for events being record I'm sure as well. You can somewhat speed up the recording of events by flushing them so that they aren't stored in the buffer and only sent periodically.

Let me know if I'm understanding your goal properly, but it sounds like the intent is to have the Dynamic Segment created programmatically. If that's the case, then there's no Amplify API that can be called on the client side to do this simultaneously when the organizationName attribute is sent off to Pinpoint. However, this could be done using a Lambda function that would set up to create/modify a segment possibly when the organizationName attribute is updated or given a value.

renso3x commented 1 day ago

Hi @cwomack,

Yes, my goal is to create the dynamic segment in Amplify. Generally, I trigger the identifyUser function only once when the user signs in to the app. However, if my goal is to update the user endpoint, would it be better to use a Lambda function instead?