Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,25 @@ internal class DatadogSDKWrapper : DatadogWrapper {
override fun clearUserInfo() {
Datadog.clearUserInfo()
}


override fun setAccountInfo(
id: String,
name: String?,
extraInfo: Map<String, Any?>
) {
Datadog.setAccountInfo(id, name, extraInfo)
}

override fun addAccountExtraInfo(
extraInfo: Map<String, Any?>
) {
Datadog.addAccountExtraInfo(extraInfo)
}

override fun clearAccountInfo() {
Datadog.clearAccountInfo()
}

override fun addRumGlobalAttribute(key: String, value: Any?) {
this.getRumMonitor().addAttribute(key, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ interface DatadogWrapper {
*/
fun clearUserInfo()

/**
* Sets the account information.
*
* @param id a unique account identifier (relevant to your business domain)
* @param name (nullable) the account name
* @param extraInfo additional information. An extra information can be
* nested up to 8 levels deep. Keys using more than 8 levels will be sanitized by SDK.
*/
fun setAccountInfo(
id: String,
name: String?,
extraInfo: Map<String, Any?>
)

/**
* Sets the account information.
* @param extraInfo: The additional information. (To set the id or name please use setAccountInfo).
*/
fun addAccountExtraInfo(
extraInfo: Map<String, Any?>
)

/**
* Clears the account information.
*/
fun clearAccountInfo()


/** Adds a global attribute.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,47 @@ class DdSdkImplementation(
promise.resolve(null)
}

/**
* Set the account information.
* @param accountInfo The account object (use builtin attributes: 'id', 'name', and any custom
* attribute inside 'extraInfo').
*/
fun setAccountInfo(accountInfo: ReadableMap, promise: Promise) {
val accountInfoMap = accountInfo.toHashMap().toMutableMap()
val id = accountInfoMap["id"] as? String
val name = accountInfoMap["name"] as? String
val extraInfo = (accountInfoMap["extraInfo"] as? Map<*, *>)?.filterKeys { it is String }
?.mapKeys { it.key as String }
?.mapValues { it.value } ?: emptyMap()

if (id != null) {
datadog.setAccountInfo(id, name, extraInfo)
}

promise.resolve(null)
}

/**
* Sets the account extra information.
* @param accountExtraInfo: The additional information. (To set the id or name please use setAccountInfo).
*/
fun addAccountExtraInfo(
accountExtraInfo: ReadableMap, promise: Promise
) {
val extraInfoMap = accountExtraInfo.toHashMap().toMutableMap()

datadog.addAccountExtraInfo(extraInfoMap)
promise.resolve(null)
}

/**
* Clears the account information.
*/
fun clearAccountInfo(promise: Promise) {
datadog.clearAccountInfo()
promise.resolve(null)
}

/**
* Set the tracking consent regarding the data collection.
* @param trackingConsent Consent, which can take one of the following values: 'pending',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ class DdSdk(
implementation.clearUserInfo(promise)
}

/**
* Set the account information.
* @param account The account object (use builtin attributes: 'id', 'name', and any custom * attribute inside 'extraInfo').
*/
@ReactMethod
override fun setAccountInfo(account: ReadableMap, promise: Promise) {
implementation.setAccountInfo(account, promise)
}

/**
* Sets the account information.
* @param extraAccountInfo: The additional information. (To set the id or name please use setAccountInfo).
*/
@ReactMethod
override fun addAccountExtraInfo(extraInfo: ReadableMap, promise: Promise) {
implementation.addAccountExtraInfo(extraInfo, promise)
}

/**
* Clears the account information.
*/
@ReactMethod
override fun clearAccountInfo(promise: Promise) {
implementation.clearAccountInfo(promise)
}

/**
* Set the tracking consent regarding the data collection.
* @param trackingConsent Consent, which can take one of the following values: 'pending',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,32 @@ class DdSdk(
implementation.clearUserInfo(promise)
}

/**
* Set the account information.
* @param account The account object (use builtin attributes: 'id', 'name', and any custom * attribute inside 'extraInfo').
*/
@ReactMethod
fun setAccountInfo(account: ReadableMap, promise: Promise) {
implementation.setAccountInfo(account, promise)
}

/**
* Sets the account information.
* @param extraAccountInfo: The additional information. (To set the id or name please use setAccountInfo).
*/
@ReactMethod
fun addAccountExtraInfo(extraInfo: ReadableMap, promise: Promise) {
implementation.addAccountExtraInfo(extraInfo, promise)
}

/**
* Clears the account information.
*/
@ReactMethod
fun clearAccountInfo(promise: Promise) {
implementation.clearAccountInfo(promise)
}

/**
* Set the tracking consent regarding the data collection.
* @param trackingConsent Consent, which can take one of the following values: 'pending',
Expand Down
32 changes: 32 additions & 0 deletions packages/core/ios/Sources/DdSdk.mm
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ + (void)initFromNative {
[self clearUserInfo:resolve reject:reject];
}

RCT_REMAP_METHOD(setAccountInfo, withAccountInfo:(NSDictionary*)accountInfo
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
[self setAccountInfo:accountInfo resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(addAccountExtraInfo, withAccountExtraInfo:(NSDictionary*)extraInfo
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
[self addAccountExtraInfo:extraInfo resolve:resolve reject:reject];
}

RCT_EXPORT_METHOD(clearAccountInfo:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
[self clearAccountInfo:resolve reject:reject];
}

RCT_REMAP_METHOD(setTrackingConsent, withTrackingConsent:(NSString*)trackingConsent
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
Expand Down Expand Up @@ -189,6 +209,18 @@ -(void)addUserExtraInfo:(NSDictionary *)extraInfo resolve:(RCTPromiseResolveBloc
[self.ddSdkImplementation addUserExtraInfoWithExtraInfo:extraInfo resolve:resolve reject:reject];
}

- (void)setAccountInfo:(NSDictionary *)accountInfo resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[self.ddSdkImplementation setAccountInfoWithAccountInfo:accountInfo resolve:resolve reject:reject];
}

- (void)clearAccountInfo:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[self.ddSdkImplementation clearAccountInfoWithResolve:resolve reject:reject];
}

-(void)addAccountExtraInfo:(NSDictionary *)extraInfo resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[self.ddSdkImplementation addAccountExtraInfoWithExtraInfo:extraInfo resolve:resolve reject:reject];
}

- (void)sendTelemetryLog:(NSString *)message attributes:(NSDictionary *)attributes config:(NSDictionary *)config resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[self.ddSdkImplementation sendTelemetryLogWithMessage:message attributes:attributes config:config resolve:resolve reject:reject];
}
Expand Down
38 changes: 38 additions & 0 deletions packages/core/ios/Sources/DdSdkImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,44 @@ public class DdSdkImplementation: NSObject {
resolve(nil)
}

@objc
public func setAccountInfo(
accountInfo: NSDictionary, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock
) {
let castedAccountInfo = castAttributesToSwift(accountInfo)
let id = castedAccountInfo["id"] as? String
let name = castedAccountInfo["name"] as? String
var extraInfo: [AttributeKey: AttributeValue] = [:]

if let extraInfoEncodable = castedAccountInfo["extraInfo"] as? AnyEncodable,
let extraInfoDict = extraInfoEncodable.value as? [String: Any]
{
extraInfo = castAttributesToSwift(extraInfoDict)
}

if let validId = id {
Datadog.setAccountInfo(id: validId, name: name, extraInfo: extraInfo)
}

resolve(nil)
}

@objc
public func addAccountExtraInfo(
extraInfo: NSDictionary, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock
) {
let castedExtraInfo = castAttributesToSwift(extraInfo)

Datadog.addAccountExtraInfo(castedExtraInfo)
resolve(nil)
}

@objc
public func clearAccountInfo(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
Datadog.clearAccountInfo()
resolve(nil)
}

@objc
public func setTrackingConsent(
trackingConsent: NSString, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock
Expand Down
9 changes: 9 additions & 0 deletions packages/core/jest/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ module.exports = {
clearUserInfo: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
setAccountInfo: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
addAccountExtraInfo: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
clearAccountInfo: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
addAttribute: jest
.fn()
.mockImplementation(() => new Promise(resolve => resolve())),
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/DdSdkReactNative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { DdRumErrorTracking } from './rum/instrumentation/DdRumErrorTracking';
import { DdBabelInteractionTracking } from './rum/instrumentation/interactionTracking/DdBabelInteractionTracking';
import { DdRumUserInteractionTracking } from './rum/instrumentation/interactionTracking/DdRumUserInteractionTracking';
import { DdRumResourceTracking } from './rum/instrumentation/resourceTracking/DdRumResourceTracking';
import { AccountInfoSingleton } from './sdk/AccountInfoSingleton/AccountInfoSingleton';
import { AttributesSingleton } from './sdk/AttributesSingleton/AttributesSingleton';
import type { Attributes } from './sdk/AttributesSingleton/types';
import { registerNativeBridge } from './sdk/DatadogInternalBridge/DdSdkInternalNativeBridge';
Expand Down Expand Up @@ -299,6 +300,71 @@ export class DdSdkReactNative {
UserInfoSingleton.getInstance().setUserInfo(updatedUserInfo);
};

/**
* Sets the account information.
* @param id: A mandatory unique account identifier (relevant to your business domain).
* @param name: The account name.
* @param extraInfo: Additional information.
* @returns a Promise.
*/
static setAccountInfo = async (accountInfo: {
id: string;
name?: string;
extraInfo?: Record<string, unknown>;
}): Promise<void> => {
InternalLog.log(
`Setting account ${JSON.stringify(accountInfo)}`,
SdkVerbosity.DEBUG
);

await DdSdk.setAccountInfo(accountInfo);
AccountInfoSingleton.getInstance().setAccountInfo(accountInfo);
};

/**
* Clears the account information.
* @returns a Promise.
*/
static clearAccountInfo = async (): Promise<void> => {
InternalLog.log('Clearing account info', SdkVerbosity.DEBUG);
await DdSdk.clearAccountInfo();
AccountInfoSingleton.getInstance().clearAccountInfo();
};

/**
* Set the account information.
* @param extraAccountInfo: The additional information. (To set the id or name please use setAccountInfo).
* @returns a Promise.
*/
static addAccountExtraInfo = async (
extraAccountInfo: Record<string, unknown>
): Promise<void> => {
InternalLog.log(
`Adding extra account info ${JSON.stringify(extraAccountInfo)}`,
SdkVerbosity.DEBUG
);

const accountInfo = AccountInfoSingleton.getInstance().getAccountInfo();
if (!accountInfo) {
InternalLog.log(
'Skipped adding Account Extra Info: Account Info is currently undefined. An account ID must be set before adding extra info. Please call setAccountInfo() first.',
SdkVerbosity.WARN
);

return;
}

const extraInfo = {
...accountInfo.extraInfo,
...extraAccountInfo
};

await DdSdk.addAccountExtraInfo(extraInfo);
AccountInfoSingleton.getInstance().addAccountExtraInfo(
extraAccountInfo
);
};

/**
* Set the tracking consent regarding the data collection.
* @param trackingConsent: One of TrackingConsent values.
Expand Down
Loading