Skip to content

Commit 69c4f1d

Browse files
committed
feat: add telemetry settings
1 parent 9511859 commit 69c4f1d

File tree

16 files changed

+211
-4
lines changed

16 files changed

+211
-4
lines changed

android/app/src/main/java/com/appzung/codepush/react/CodePushConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ public class CodePushConstants {
3232
public static final String LATEST_ROLLBACK_TIME_KEY = "time";
3333
public static final String LATEST_ROLLBACK_COUNT_KEY = "count";
3434
public static final String CLIENT_UNIQUE_ID_KEY = "clientUniqueId";
35+
public static final String TELEMETRY_ENABLED_KEY = "telemetryEnabled";
3536
}

android/app/src/main/java/com/appzung/codepush/react/CodePushNativeModule.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
public class CodePushNativeModule extends ReactContextBaseJavaModule {
4343
private String mBinaryContentsHash = null;
4444
private String mClientUniqueId = null;
45+
private boolean mTelemetryEnabled = true;
4546
private LifecycleEventListener mLifecycleEventListener = null;
4647
private int mMinimumBackgroundDuration = 0;
4748

@@ -71,6 +72,17 @@ public CodePushNativeModule(ReactApplicationContext reactContext, CodePush codeP
7172
mClientUniqueId = UUID.randomUUID().toString();
7273
preferences.edit().putString(CodePushConstants.CLIENT_UNIQUE_ID_KEY, mClientUniqueId).apply();
7374
}
75+
76+
if (preferences.contains(CodePushConstants.TELEMETRY_ENABLED_KEY)) {
77+
mTelemetryEnabled = preferences.getBoolean(CodePushConstants.TELEMETRY_ENABLED_KEY, true);
78+
} else {
79+
int defaultTelemetryEnabledResId = reactContext.getResources().getIdentifier("CodePushDefaultTelemetryEnabled", "bool", reactContext.getPackageName());
80+
if (defaultTelemetryEnabledResId != 0) {
81+
mTelemetryEnabled = reactContext.getResources().getBoolean(defaultTelemetryEnabledResId);
82+
} else {
83+
mTelemetryEnabled = true;
84+
}
85+
}
7486
}
7587

7688
@Override
@@ -396,6 +408,7 @@ public void getConfiguration(Promise promise) {
396408
configMap.putString("clientUniqueId", mClientUniqueId);
397409
configMap.putString("releaseChannelPublicId", mCodePush.getReleaseChannelPublicId());
398410
configMap.putString("serverUrl", mCodePush.getServerUrl());
411+
configMap.putBoolean("telemetryEnabled", mTelemetryEnabled);
399412

400413
// The binary hash may be null in debug builds
401414
if (mBinaryContentsHash != null) {
@@ -745,6 +758,24 @@ public void resetClientUniqueId(Promise promise) {
745758
}
746759
}
747760

761+
@ReactMethod
762+
public void setTelemetryEnabled(boolean enabled, Promise promise) {
763+
try {
764+
SharedPreferences preferences = mCodePush.getContext().getSharedPreferences(CodePushConstants.CODE_PUSH_PREFERENCES, 0);
765+
preferences.edit().putBoolean(CodePushConstants.TELEMETRY_ENABLED_KEY, enabled).apply();
766+
mTelemetryEnabled = enabled;
767+
promise.resolve(null);
768+
} catch (Exception e) {
769+
CodePushUtils.log(e);
770+
promise.reject(e);
771+
}
772+
}
773+
774+
@ReactMethod
775+
public void getTelemetryEnabled(Promise promise) {
776+
promise.resolve(mTelemetryEnabled);
777+
}
778+
748779
@ReactMethod
749780
public void addListener(String eventName) {
750781
// Set up any upstream listeners or background tasks as necessary

docs/advanced-usage.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
## Advanced usage
22

3+
### Privacy / GDPR
4+
5+
#### Telemetry
6+
7+
CodePush collects anonymous usage data by default to help improve the service (e.g. to provide you with analytics).
8+
To be fully compliant with GDPR and other privacy regulations, you may want to disable this telemetry collection.
9+
10+
##### Disabling telemetry by default
11+
12+
You can disable telemetry collection by default by configuring your native app:
13+
14+
**Android:**
15+
16+
Add this to your `android/app/build.gradle` file:
17+
18+
```groovy
19+
android {
20+
// ...
21+
defaultConfig {
22+
// ...
23+
resValue "bool", "CodePushDefaultTelemetryEnabled", "false"
24+
}
25+
}
26+
```
27+
28+
**iOS:**
29+
30+
Add this to your `ios/YourApp/Info.plist` file:
31+
32+
```
33+
<key>CodePushDefaultTelemetryEnabled</key>
34+
<false/>
35+
```
36+
37+
##### Managing telemetry during runtime
38+
39+
You can also programmatically check or change telemetry status during app runtime:
40+
41+
```typescript
42+
import * as CodePush from '@appzung/react-native-code-push';
43+
44+
// Check if telemetry is enabled
45+
CodePush.getTelemetryEnabled().then((enabled) => {
46+
console.log('Telemetry is ' + (enabled ? 'enabled' : 'disabled'));
47+
});
48+
49+
// Enable telemetry
50+
CodePush.setTelemetryEnabled(true);
51+
52+
// Disable telemetry
53+
CodePush.setTelemetryEnabled(false);
54+
```
55+
56+
This setting will persist across app sessions.
57+
358
### Multiple environments
459

560
#### Staging / Production
@@ -43,7 +98,7 @@ In order to achieve this kind of workflow, all you need to do is specify the rel
4398
```javascript
4499
// Imagine that "userProfile" is a prop that this component received
45100
// which includes the release channel public ID that the current user should use.
46-
codePush.sync({ releaseChannelPublicId: userProfile.RELEASE_CHANNEL_PUBLIC_ID });
101+
CodePush.sync({ releaseChannelPublicId: userProfile.RELEASE_CHANNEL_PUBLIC_ID });
47102
```
48103

49104
With that change in place, now it's just a matter of choosing how your app determines the right release channel for the current user. In practice, there are typically two solutions for this:

docs/api-js/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@
4343
- [disallowRestart](functions/disallowRestart.md)
4444
- [getClientUniqueId](functions/getClientUniqueId.md)
4545
- [getLogLevel](functions/getLogLevel.md)
46+
- [getTelemetryEnabled](functions/getTelemetryEnabled.md)
4647
- [getUpdateMetadata](functions/getUpdateMetadata.md)
4748
- [notifyAppReady](functions/notifyAppReady.md)
4849
- [resetClientUniqueId](functions/resetClientUniqueId.md)
4950
- [restartApp](functions/restartApp.md)
5051
- [setLogLevel](functions/setLogLevel.md)
52+
- [setTelemetryEnabled](functions/setTelemetryEnabled.md)
5153
- [sync](functions/sync.md)
5254
- [withCodePush](functions/withCodePush.md)
5355

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / getTelemetryEnabled
6+
7+
# Function: getTelemetryEnabled()
8+
9+
> **getTelemetryEnabled**(): `Promise`\<`boolean`\>
10+
11+
Gets the current telemetry enabled status.
12+
13+
When setTelemetryEnabled has never been enabled, returns the default value set in your configuration.
14+
15+
## Returns
16+
17+
`Promise`\<`boolean`\>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / setTelemetryEnabled
6+
7+
# Function: setTelemetryEnabled()
8+
9+
> **setTelemetryEnabled**(`enabled`): `Promise`\<`void`\>
10+
11+
Controls telemetry reporting.
12+
13+
## Parameters
14+
15+
### enabled
16+
17+
`boolean`
18+
19+
When false, updates on this device will stop appearing as failed, pending or succeeded in analytics.
20+
21+
## Returns
22+
23+
`Promise`\<`void`\>

ios/CodePush/CodePush.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
@property (readonly) NSString *clientUniqueId;
105105
@property (copy) NSString *serverURL;
106106
@property (copy) NSString *publicKey;
107+
@property (nonatomic) BOOL telemetryEnabled;
107108

108109
+ (instancetype)current;
109110

ios/CodePush/CodePush.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,20 @@ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
10491049
resolve([[CodePushConfig current] clientUniqueId]);
10501050
}
10511051

1052+
RCT_EXPORT_METHOD(setTelemetryEnabled:(BOOL)enabled
1053+
resolver:(RCTPromiseResolveBlock)resolve
1054+
rejecter:(RCTPromiseRejectBlock)reject)
1055+
{
1056+
[[CodePushConfig current] setTelemetryEnabled:enabled];
1057+
resolve(nil);
1058+
}
1059+
1060+
RCT_EXPORT_METHOD(getTelemetryEnabled:(RCTPromiseResolveBlock)resolve
1061+
rejecter:(RCTPromiseRejectBlock)reject)
1062+
{
1063+
resolve(@([[CodePushConfig current] telemetryEnabled]));
1064+
}
1065+
10521066
#pragma mark - JavaScript-exported module methods (Private)
10531067

10541068
/*

ios/CodePush/CodePushConfig.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ @implementation CodePushConfig {
1313
static NSString * const ReleaseChannelPublicIDConfigKey = @"releaseChannelPublicId";
1414
static NSString * const ServerURLConfigKey = @"serverUrl";
1515
static NSString * const PublicKeyKey = @"publicKey";
16+
static NSString * const TelemetryEnabledKey = @"telemetryEnabled";
1617

1718
+ (instancetype)current
1819
{
@@ -49,6 +50,16 @@ - (instancetype)init
4950
[userDefaults synchronize];
5051
}
5152

53+
NSNumber *defaultTelemetryEnabled = [infoDictionary objectForKey:@"CodePushDefaultTelemetryEnabled"];
54+
BOOL telemetryEnabled;
55+
if ([userDefaults objectForKey:TelemetryEnabledKey] != nil) {
56+
telemetryEnabled = [userDefaults boolForKey:TelemetryEnabledKey];
57+
} else if (defaultTelemetryEnabled != nil) {
58+
telemetryEnabled = [defaultTelemetryEnabled boolValue];
59+
} else {
60+
telemetryEnabled = YES;
61+
}
62+
5263
if (!serverURL) {
5364
serverURL = @"https://codepush.appzung.com/";
5465
}
@@ -64,6 +75,7 @@ - (instancetype)init
6475
CPLog(@"Executing CodePush with a signing public key.");
6576
[_configDictionary setObject:publicKey forKey:PublicKeyKey];
6677
}
78+
[_configDictionary setObject:@(telemetryEnabled) forKey:TelemetryEnabledKey];
6779

6880
return self;
6981
}
@@ -103,6 +115,19 @@ - (NSString *)publicKey
103115
return [_configDictionary objectForKey:PublicKeyKey];
104116
}
105117

118+
- (BOOL)telemetryEnabled
119+
{
120+
return [[_configDictionary objectForKey:TelemetryEnabledKey] boolValue];
121+
}
122+
123+
- (void)setTelemetryEnabled:(BOOL)telemetryEnabled
124+
{
125+
[_configDictionary setValue:@(telemetryEnabled) forKey:TelemetryEnabledKey];
126+
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
127+
[preferences setBool:telemetryEnabled forKey:TelemetryEnabledKey];
128+
[preferences synchronize];
129+
}
130+
106131
- (void)setAppVersion:(NSString *)appVersion
107132
{
108133
[_configDictionary setValue:appVersion forKey:AppVersionConfigKey];

src/checkForUpdates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function checkForUpdate(
3939
* release channels (e.g. an early access release channel for insiders).
4040
*/
4141
const config: Configuration = releaseChannelPublicId ? { ...nativeConfig, releaseChannelPublicId } : nativeConfig;
42-
const sdk = new CodePushApiSdk(requestFetchAdapter, config);
42+
const sdk = new CodePushApiSdk(requestFetchAdapter, log, config);
4343

4444
const localPackage = await getCurrentPackage();
4545

0 commit comments

Comments
 (0)