launchdarkly / js-client-sdk

LaunchDarkly Client-side SDK for Browser JavaScript
Other
109 stars 62 forks source link

Count visitor only when they get to see the flag/AB test #241

Closed cafesanu closed 2 years ago

cafesanu commented 2 years ago

Is your feature request related to a problem? Please describe. I am unsure if this is a feature request or this already exists, but I couldn't find a solution so here I am... Let me know if this exists in any way 🙂

Describe the solution you'd like Solution 1 (ideal):

Solution 2 (not ideal, but would work):

Describe alternatives you've considered I have considered passing information via the custom key in the LDUser object with the key equal to the path of the current page and the value being true. So if the user visits pageA and PageB, I can update the custom key each time a user arrives to a new page. Somthin like

const path = getUriPath()
client.identify({
  custom: {
    [path]: true
  }
});

this way, the custom object would look something like this eventually:

  custom: {
    pageA: true
    pageB: true
  }

However:

  1. This seems hacky
  2. I don't think there is a way to filter in the experimentation tab by user attributes

Additional context

image

Thank you very much!

eli-darkly commented 2 years ago

I'm not sure I understand how your application is using the SDK. Specifically, in this part—

Since LaunchDarkly asks for all the flags at the same time, I get FlagA and FlagB variations in Page A

—it is true that "LaunchDarkly asks for all the flags at the same time" in terms of how the network request is done by the SDK; but that does not cause the analytics data to record a request for every flag, as it sounds like you are assuming it does. The evaluation count per flag is updated when you explicitly call allFlags to get all flag values at once, or call variation for a specific flag. I'm guessing that maybe you are calling allFlags? In any case, my point is that whatever you're doing to cause the results you're describing is definitely not the only way to use the SDK. It is designed to allow you to track usage independently per flag— otherwise analytics would not be very useful.

If you want to call allFlags but do not want it to affect the request count for any flag, you can set the sendEventsOnlyForVariation option as described here.

cafesanu commented 2 years ago

That did the trick! I was not aware that requesting one single variation would record a visitor just for that variation. The part that confused me was this in here https://launchdarkly.github.io/js-client-sdk/interfaces/_launchdarkly_js_client_sdk_.ldclient.html#variation

In the client-side JavaScript SDKs, this is always a fast synchronous operation because all of the feature flag values for the current user have already been loaded into memory.

So I thought getting all the flags via allFlags or getting one single variation was the same at the end of the day since all flags are being loaded anyway.

I ended up using sendEventsOnlyForVariation: true in the LaunchdarklyProvider, getting all flags at the same time on load (since now the are not being added to analytics and calling client.variation to "record" the visitor whenever I get to the actual test page.

Thank you very much! You truly helped :)