Skip to content

Commit 274f98b

Browse files
authored
Added a programmatic method to switch Firebase tracking on/off (#349)
* Added a programmatic method to switch Firebase tracking on/off * Added javadoc for tracking enable method
1 parent d0541d9 commit 274f98b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ static void onAppFocus() {
956956

957957
getCurrentPermissionState(appContext).refreshAsTo();
958958

959-
if(trackFirebaseAnalytics != null)
959+
if(trackFirebaseAnalytics != null && getFirebaseAnalyticsEnabled(appContext))
960960
trackFirebaseAnalytics.trackInfluenceOpenEvent();
961961
}
962962

@@ -1487,7 +1487,7 @@ public void run() {
14871487
}
14881488
});
14891489

1490-
if(trackFirebaseAnalytics != null)
1490+
if(trackFirebaseAnalytics != null && getFirebaseAnalyticsEnabled(appContext))
14911491
trackFirebaseAnalytics.trackOpenedEvent(openedResult);
14921492
}
14931493

@@ -1501,7 +1501,7 @@ static void handleNotificationReceived(JSONArray data, boolean displayed, boolea
15011501
OSNotificationOpenResult openResult = generateOsNotificationOpenResult(data, displayed, fromAlert);
15021502
mInitBuilder.mNotificationReceivedHandler.notificationReceived(openResult.notification);
15031503

1504-
if(trackFirebaseAnalytics != null)
1504+
if(trackFirebaseAnalytics != null && getFirebaseAnalyticsEnabled(appContext))
15051505
trackFirebaseAnalytics.trackReceivedEvent(openResult);
15061506

15071507
}
@@ -1634,6 +1634,26 @@ static void updateUserIdDependents(String userId) {
16341634
OneSignalChromeTab.setup(appContext, appId, userId, AdvertisingIdProviderGPS.getLastValue());
16351635
}
16361636

1637+
/**
1638+
* By default, OneSignal does not auto-track notification events through Firebase.
1639+
* Call this method with {@code true} to start tracking.
1640+
*
1641+
* @see <a href="https://documentation.onesignal.com/docs/firebase-analytics">Firebase Analytics | OneSignal Docs</a>
1642+
* @param enable whether to enable Firebase tracking
1643+
*/
1644+
public static void enableFirebaseAnalytics(boolean enable) {
1645+
if (appContext == null)
1646+
return;
1647+
1648+
OneSignalPrefs.saveBool(OneSignalPrefs.PREFS_ONESIGNAL,
1649+
OneSignalPrefs.PREFS_GT_FIREBASE_TRACKING_ENABLED, enable);
1650+
}
1651+
1652+
static boolean getFirebaseAnalyticsEnabled(Context context) {
1653+
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL,
1654+
OneSignalPrefs.PREFS_GT_FIREBASE_TRACKING_ENABLED,false);
1655+
}
1656+
16371657
// If true(default) - Device will always vibrate unless the device is in silent mode.
16381658
// If false - Device will only vibrate when the device is set on it's vibrate only mode.
16391659
/**

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalPrefs.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class OneSignalPrefs {
2525
static final String PREFS_GT_SOUND_ENABLED = "GT_SOUND_ENABLED";
2626
static final String PREFS_OS_LAST_SESSION_TIME = "OS_LAST_SESSION_TIME";
2727
static final String PREFS_GT_VIBRATE_ENABLED = "GT_VIBRATE_ENABLED";
28+
static final String PREFS_GT_FIREBASE_TRACKING_ENABLED = "GT_FIREBASE_TRACKING_ENABLED";
2829
static final String PREFS_OS_FILTER_OTHER_GCM_RECEIVERS = "OS_FILTER_OTHER_GCM_RECEIVERS";
2930
static final String PREFS_GT_APP_ID = "GT_APP_ID";
3031
static final String PREFS_GT_PLAYER_ID = "GT_PLAYER_ID";

0 commit comments

Comments
 (0)