Skip to content

Commit c4d7097

Browse files
[STRATCONN-39] Enable manual screen tracking (#27)
* Try to enable manual screen tracking * Update SNAPSHOT for release * Remove new package dependencies * Move variables to global vars Co-authored-by: Stian Jensen <[email protected]>
1 parent bffdb1d commit c4d7097

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.1.0 / 2020-02-20
2+
==================
3+
* Add support for explicitly tracking screen calls.
4+
15
2.0.0 / 2019-11-15
26
==================
37
*(Supports Android 29+ and Gradle 3.2.1+)*
@@ -37,4 +41,4 @@ Version 1.0.0 (7th September, 2017)
3741
===================================
3842
*(Supports analytics-android 4.2.6 and Firebase Core 11.2.0)*
3943

40-
* Initial Release
44+
* Initial Release

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GROUP=com.segment.analytics.android.integrations
22

3-
VERSION=2.0.0-SNAPSHOT
3+
VERSION=2.1.0-SNAPSHOT
44

55
POM_ARTIFACT_ID=firebase
66
POM_PACKAGING=aar

src/main/java/com/segment/analytics/android/integrations/firebase/FirebaseIntegration.java

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.segment.analytics.integrations.Integration;
1818
import com.segment.analytics.integrations.Logger;
1919
import com.segment.analytics.integrations.TrackPayload;
20+
import com.segment.analytics.integrations.ScreenPayload;
2021

2122
import java.util.HashMap;
2223
import java.util.Map;
@@ -64,6 +65,7 @@ public String key() {
6465
private final Logger logger;
6566
private final FirebaseAnalytics firebaseAnalytics;
6667
private static final Map<String, String> EVENT_MAPPER = createEventMap();
68+
private Activity currentActivity;
6769

6870
private static Map<String, String> createEventMap() {
6971
Map<String, String> EVENT_MAPPER = new HashMap<>();
@@ -122,6 +124,20 @@ public void onActivityResumed(Activity activity) {
122124
}
123125
}
124126

127+
@Override
128+
public void onActivityStarted(Activity activity) {
129+
super.onActivityStarted(activity);
130+
131+
this.currentActivity = activity;
132+
}
133+
134+
@Override
135+
public void onActivityStopped(Activity activity) {
136+
super.onActivityStopped(activity);
137+
138+
this.currentActivity = null;
139+
}
140+
125141
@Override
126142
public void identify(IdentifyPayload identify) {
127143
super.identify(identify);
@@ -156,6 +172,15 @@ public void track(TrackPayload track) {
156172
logger.verbose("firebaseAnalytics.logEvent(%s, %s);", eventName, formattedProperties);
157173
}
158174

175+
@Override
176+
public void screen(ScreenPayload screen) {
177+
super.screen(screen);
178+
179+
if (this.currentActivity != null) {
180+
firebaseAnalytics.setCurrentScreen(this.currentActivity, screen.name(), null);
181+
}
182+
}
183+
159184
private static Bundle formatProperties(Properties properties) {
160185
Bundle bundle = new Bundle();
161186
if ((properties.revenue() != 0 || properties.total() != 0)

src/test/java/com/segment/analytics/android/integration/firebase/FirebaseTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.segment.analytics.android.integration.firebase;
22

3+
import android.app.Activity;
34
import android.content.Context;
45
import android.os.Bundle;
56

@@ -8,6 +9,7 @@
89
import com.segment.analytics.android.integrations.firebase.FirebaseIntegration;
910
import com.segment.analytics.integrations.IdentifyPayload;
1011
import com.segment.analytics.integrations.Logger;
12+
import com.segment.analytics.integrations.ScreenPayload;
1113
import com.segment.analytics.integrations.TrackPayload;
1214

1315
import org.json.JSONException;
@@ -33,6 +35,8 @@
3335
import static com.segment.analytics.Analytics.LogLevel.VERBOSE;
3436
import static org.mockito.Matchers.argThat;
3537
import static org.mockito.Matchers.eq;
38+
import static org.mockito.Mockito.any;
39+
import static org.mockito.Mockito.isNull;
3640
import static org.mockito.Mockito.verify;
3741

3842
@RunWith(RobolectricTestRunner.class)
@@ -143,6 +147,16 @@ public void trackWithEventNameTransformation() {
143147
verify(firebase).logEvent(eq("foo_bar"), bundleEq(expected));
144148
}
145149

150+
@Test
151+
public void trackScreenWithName() {
152+
final Activity activity = PowerMockito.mock(Activity.class);
153+
integration.onActivityStarted(activity);
154+
155+
integration.screen(new ScreenPayload.Builder().anonymousId("1234").name("home_screen").build());
156+
157+
verify(firebase).setCurrentScreen(any(Activity.class), eq("home_screen"), (String) isNull());
158+
}
159+
146160
/**
147161
* Uses the string representation of the object. Useful for JSON objects.
148162
* @param expected Expected object
@@ -187,4 +201,4 @@ public boolean matches(Bundle bundle) {
187201
}
188202

189203
}
190-
}
204+
}

0 commit comments

Comments
 (0)