Skip to content

Commit 766f87d

Browse files
authored
Merge pull request #410 from Countly/deviceId-interface
Device Id Interface Added
2 parents 7636571 + a76478b commit 766f87d

File tree

8 files changed

+133
-6
lines changed

8 files changed

+133
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
* During an internal timer tick
55
* Upon flushing the event queue
66
* Added Content feature methods:
7-
* enterContentZone, to start Content checks (Experimental!)
8-
* exitContentZone, to stop Content checks (Experimental!)
7+
* `enterContentZone`, to start Content checks (Experimental!)
8+
* `exitContentZone`, to stop Content checks (Experimental!)
9+
* Added `Countly.deviceId.setID` method for changing device ID based on the device ID type
910
* Mitigated an issue where a session could have started while the app was in the background when the device ID was changed (non-merge).
10-
11+
* Deprecated following SDK calls:
12+
* `Countly.getCurrentDeviceId` (replaced with: `Countly.deviceId.getID`)
13+
* `Countly.getDeviceIDType` (replaced with: `Countly.deviceId.getType`)
14+
* `Countly.changeDeviceId` (replaced with: `Countly.deviceId.setID`)
1115
* Android Specific Changes:
1216
* ! Minor breaking change ! Unsupported types for user properties will now be omitted, they won't be converted to strings.
1317
* Disabled caching for webviews.

Countly.d.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ declare module "countly-sdk-react-native-bridge" {
402402
export function disableLocation(): string | void;
403403

404404
/**
405-
*
405+
* @deprecated use 'Countly.deviceId.getID' instead of 'Countly.getCurrentDeviceId'
406+
*
406407
* Get currently used device Id.
407408
* Should be called after Countly init
408409
*
@@ -411,6 +412,8 @@ declare module "countly-sdk-react-native-bridge" {
411412
export function getCurrentDeviceId(): Promise<string> | string;
412413

413414
/**
415+
* @deprecated use 'Countly.deviceId.getType' instead of 'Countly.getDeviceIDType'
416+
*
414417
* Get currently used device Id type.
415418
* Should be called after Countly init
416419
*
@@ -419,6 +422,8 @@ declare module "countly-sdk-react-native-bridge" {
419422
export function getDeviceIDType(): Promise<DeviceIdType> | null;
420423

421424
/**
425+
* @deprecated use 'Countly.deviceId.setID' instead of 'Countly.changeDeviceId'
426+
*
422427
* Change the current device id
423428
*
424429
* @param {string} newDeviceID id new device id
@@ -427,6 +432,34 @@ declare module "countly-sdk-react-native-bridge" {
427432
*/
428433
export function changeDeviceId(newDeviceID: string, onServer: boolean): string | void;
429434

435+
namespace deviceId {
436+
/**
437+
*
438+
* Get currently used device ID.
439+
* Should be called after Countly init
440+
*
441+
* @returns {string | null} device ID or null
442+
*/
443+
export function getID(): Promise<string> | string;
444+
445+
/**
446+
*
447+
* Get currently used device ID type.
448+
* Should be called after Countly init
449+
*
450+
* @return {DeviceIdType | null} deviceIdType or null
451+
*/
452+
export function getType(): Promise<DeviceIdType> | null;
453+
454+
/**
455+
* Sets device ID according to the device ID Type.
456+
* If previous ID was Developer Supplied sets it without merge, otherwise with merge.
457+
*
458+
* @param {string} newDeviceID device ID to set
459+
*/
460+
export function setID(newDeviceID: string): void;
461+
}
462+
430463
/**
431464
*
432465
* Set to "true" if you want HTTP POST to be used for all requests

Countly.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import CountlyConfig from "./CountlyConfig.js";
1010
import CountlyState from "./CountlyState.js";
1111
import Feedback from "./Feedback.js";
1212
import Event from "./Event.js";
13+
import DeviceId from "./DeviceId.js";
1314
import * as L from "./Logger.js";
1415
import * as Utils from "./Utils.js";
1516
import * as Validate from "./Validators.js";
@@ -26,6 +27,7 @@ CountlyState.eventEmitter = eventEmitter;
2627

2728
Countly.feedback = new Feedback(CountlyState);
2829
Countly.events = new Event(CountlyState);
30+
Countly.deviceId = new DeviceId(CountlyState);
2931

3032
let _isCrashReportingEnabled = false;
3133

@@ -458,7 +460,8 @@ Countly.disableLocation = function () {
458460
};
459461

460462
/**
461-
*
463+
* @deprecated use 'Countly.deviceId.getID' instead of 'Countly.getCurrentDeviceId'
464+
*
462465
* Get currently used device Id.
463466
* Should be called after Countly init
464467
*
@@ -476,6 +479,8 @@ Countly.getCurrentDeviceId = async function () {
476479
};
477480

478481
/**
482+
* @deprecated use 'Countly.deviceId.getType' instead of 'Countly.getDeviceIDType'
483+
*
479484
* Get currently used device Id type.
480485
* Should be called after Countly init
481486
*
@@ -492,7 +497,9 @@ Countly.getDeviceIDType = async function () {
492497
};
493498

494499
/**
495-
* Change the current device id
500+
* @deprecated use 'Countly.deviceId.setID' instead of 'Countly.changeDeviceId' for setting device ID.
501+
*
502+
* Change the current device ID
496503
*
497504
* @param {string} newDeviceID id new device id
498505
* @param {boolean} onServer merge device id

DeviceId.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as L from "./Logger.js";
2+
import * as Utils from "./Utils.js";
3+
4+
class DeviceId {
5+
#state;
6+
7+
constructor(state) {
8+
this.#state = state;
9+
}
10+
11+
/**
12+
*
13+
* Get currently used device ID.
14+
* Should be called after Countly init
15+
*
16+
* @returns {string | null} device ID or null
17+
*/
18+
getID = async function () {
19+
if (!this.#state.isInitialized) {
20+
L.w("getID, 'init' must be called before 'getID'");
21+
return null;
22+
}
23+
L.d("getID, Getting current device ID");
24+
const result = await this.#state.CountlyReactNative.getCurrentDeviceId();
25+
return result;
26+
};
27+
28+
/**
29+
* Get currently used device ID type.
30+
* Should be called after Countly init
31+
*
32+
* @return {DeviceIdType | null} deviceIdType or null
33+
*/
34+
getType = async function () {
35+
if (!this.#state.isInitialized) {
36+
L.w("getType, 'init' must be called before 'getType'");
37+
return null;
38+
}
39+
L.d("getType, Getting device ID type");
40+
const result = await this.#state.CountlyReactNative.getDeviceIDType();
41+
return Utils.intToDeviceIDType(result);
42+
};
43+
44+
/**
45+
* Sets device ID according to the device ID Type.
46+
* If previous ID was Developer Supplied sets it without merge, otherwise with merge.
47+
*
48+
* @param {string} newDeviceID - device ID to set
49+
*/
50+
setID = function (newDeviceID) {
51+
if (!this.#state.isInitialized) {
52+
L.w("setID, 'init' must be called before 'setID'");
53+
return;
54+
}
55+
// Check if newDeviceID is not a string
56+
if (!newDeviceID || typeof newDeviceID !== "string" || newDeviceID.length === 0) {
57+
L.w("setID, provided device ID is not a valid string:[" + newDeviceID + "]");
58+
return;
59+
}
60+
L.d(`setID, Setting device id as: [${newDeviceID}]`);
61+
this.#state.CountlyReactNative.setID(newDeviceID);
62+
};
63+
}
64+
65+
export default DeviceId;

android/src/main/java/ly/count/android/sdk/react/CountlyReactNative.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,11 @@ public void exitContentZone() {
16101610
Countly.sharedInstance().contents().exitContentZone();
16111611
}
16121612

1613+
@ReactMethod
1614+
public void setID(String newDeviceID) {
1615+
Countly.sharedInstance().deviceId().setID(newDeviceID);
1616+
}
1617+
16131618
@ReactMethod
16141619
public void setCustomMetrics(ReadableArray args) {
16151620
Map<String, String> customMetric = new HashMap<>();

example/CountlyRNExample/DeviceID.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ const changeDeviceId = () => {
1313
Countly.changeDeviceId("02d56d66-6a39-482d-aff0-d14e4d5e5fda", true);
1414
};
1515

16+
const setDeviceId = () => {
17+
Countly.deviceId.setID("TestingDeviceIDValue");
18+
};
19+
1620
function DeviceIDScreen({ navigation }) {
1721
return (
1822
<SafeAreaView>
1923
<ScrollView>
2024
<CountlyButton title="Temporary Device ID Mode" onPress={temporaryDeviceIdMode} color={lightOrange} />
2125
<CountlyButton title="Change Device ID" onPress={changeDeviceId} color={lightOrange} />
26+
<CountlyButton title="Set Device ID" onPress={setDeviceId} color={lightOrange} />
2227
</ScrollView>
2328
</SafeAreaView>
2429
);

ios/src/CountlyReactNative.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ typedef void (^Result)(id _Nullable result);
5656
- (void)enterContentZone;
5757
- (void)exitContentZone;
5858

59+
- (void)setID;
60+
5961
#ifndef COUNTLY_EXCLUDE_PUSHNOTIFICATIONS
6062
- (void)notificationCallback:(NSString *_Nullable)notificationJson;
6163
+ (void)startObservingNotifications;

ios/src/CountlyReactNative.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ - (void) populateConfig:(id) json {
257257
}
258258
}
259259

260+
RCT_EXPORT_METHOD(setID : (NSString *)newDeviceID) {
261+
dispatch_async(dispatch_get_main_queue(), ^{
262+
[Countly.sharedInstance setID:newDeviceID];
263+
});
264+
}
265+
260266
RCT_EXPORT_METHOD(recordEvent : (NSDictionary *)arguments) {
261267
dispatch_async(dispatch_get_main_queue(), ^{
262268
NSString *eventName = [arguments objectForKey:@"n"];

0 commit comments

Comments
 (0)