Skip to content

Add onSessionChangeCallback to react-native tracker #1436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dnedo
Copy link

@dnedo dnedo commented Jun 3, 2025

This PR resolves #1432 by adding the ability to pass an onSessionUpdateCallback callback to the react-native tracker.

This has worked for our team when used as a patch. However, please let me know if there are any further changes required to get this merged into the library. For example, it looks like the session plugin exposes the startNewSession method, which as far as I can tell is not called anywhere right now. Perhaps the team has additional context or future plans that would require adjusting the implementation to account for sessions updated this way?

@snowplowcla
Copy link

Thanks for your pull request. Is this your first contribution to a Snowplow open source project? Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://docs.snowplowanalytics.com/docs/contributing/contributor-license-agreement/ to learn more and sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.

@snowplowcla snowplowcla added the cla:no [Auto generated] Snowplow Contributor License Agreement has not been signed. label Jun 3, 2025
@matus-tomlein
Copy link
Contributor

This looks great, thank you for the contribution!

Can I please ask you to add a unit test in the session.test.ts file in the RN tracker project. Something like this would do:

  it('calls onSessionUpdateCallback when a new session starts', async () => {
    jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));

    const callback = jest.fn();

    const sessionPlugin = await newSessionPlugin({
      asyncStorage: AsyncStorage,
      namespace: 'test-callback',
      foregroundSessionTimeout: 5,
      backgroundSessionTimeout: 5,
      onSessionUpdateCallback: callback,
    });

    const tracker = trackerCore({ corePlugins: [sessionPlugin.plugin] });

    tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
    expect(callback).toHaveBeenCalledTimes(1);
    const initialState = await sessionPlugin.getSessionState();
    expect(callback).toHaveBeenCalledWith(expect.objectContaining({
      sessionId: initialState.sessionId,
    }));

    // Advance time to force session timeout
    jest.setSystemTime(new Date('2022-04-17T00:00:10.000Z'));

    tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
    expect(callback).toHaveBeenCalledTimes(2);
    const updatedState = await sessionPlugin.getSessionState();
    expect(callback).toHaveBeenCalledWith(expect.objectContaining({
      sessionId: updatedState.sessionId,
    }));
    expect(initialState.sessionId).not.toEqual(updatedState.sessionId);
  });

About the startNewSession method – it's true that it is not used/exposed anywhere, but that is an oversight. The thinking with that function was to expose it from the tracker so that users can force the start of a new session.

@matus-tomlein
Copy link
Contributor

Also please sign the CLA in order for us to be able to integrate this, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:no [Auto generated] Snowplow Contributor License Agreement has not been signed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for onSessionUpdateCallback in the react-native tracker
3 participants