Skip to content

Commit

Permalink
Merge pull request #726 from mateo-villa/mateo-villa/RUM-5418/add-ext…
Browse files Browse the repository at this point in the history
…ra-user-info

[RUM-5418] Add RN API to append to existing user info
  • Loading branch information
marco-saia-datadog authored Oct 7, 2024
2 parents 2365f6d + eae542b commit 774e0f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/core/jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
setUser: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
addUserExtraInfo: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
setAttributes: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/DdSdkReactNative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ export class DdSdkReactNative {
UserInfoSingleton.getInstance().setUserInfo(user);
};

/**
* Set the user information.
* @param extraUserInfo: The extra info object (use builtin attributes: 'id', 'email', 'name', and/or any custom attribute).
* @returns a Promise.
*/
static addUserExtraInfo = async (
extraUserInfo: UserInfo
): Promise<void> => {
InternalLog.log(
`Adding extra user info ${JSON.stringify(extraUserInfo)}`,
SdkVerbosity.DEBUG
);
const userInfo = UserInfoSingleton.getInstance().getUserInfo();
const updatedUserInfo = { ...userInfo, ...extraUserInfo };
await DdSdk.setUser(updatedUserInfo);
UserInfoSingleton.getInstance().setUserInfo(updatedUserInfo);
};

/**
* Set the tracking consent regarding the data collection.
* @param trackingConsent: One of TrackingConsent values.
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/__tests__/DdSdkReactNative.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,25 @@ describe('DdSdkReactNative', () => {
});
});

describe('addUserExtraInfo', () => {
it('calls SDK method when addUserExtraInfo, and updates the user in UserProvider', async () => {
// GIVEN
await DdSdkReactNative.setUser({ id: 'id' });
const extraInfo = { id: 'updatedId', foo: 'bar' };

// WHEN
await DdSdkReactNative.addUserExtraInfo(extraInfo);

// THEN
expect(DdSdk.setUser).toHaveBeenCalledTimes(2);
expect(DdSdk.setUser).toHaveBeenCalledWith(extraInfo);
expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual({
id: 'updatedId',
foo: 'bar'
});
});
});

describe('setTrackingConsent', () => {
it('calls SDK method when setTrackingConsent', async () => {
// GIVEN
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export type DdSdkType = {
*/
setUser(user: object): Promise<void>;

/**
* Add additional user information.
* @param extraUserInfo: The extra info object (use builtin attributes: 'id', 'email', 'name', and/or any custom attribute).
*/
addUserExtraInfo(extraUserInfo: object): Promise<void>;

/**
* Set the tracking consent regarding the data collection.
* @param trackingConsent: Consent, which can take one of the following values: 'pending', 'granted', 'not_granted'.
Expand Down

0 comments on commit 774e0f9

Please sign in to comment.