Skip to content

Commit

Permalink
feat:page attributes (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
styluo authored Dec 14, 2022
1 parent 33eb9ea commit 6d8d899
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 701 deletions.
6 changes: 3 additions & 3 deletions demos/demo-autotrack/src/main/assets/autotrackHybridTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,21 @@
sendMockEvent(event);
}

function sendMockPageAttributesEvent() {
function sendMockPageWithAttributesEvent() {
let event = {
"deviceId": "7196f014-d7bc-4bd8-b920-757cb2375ff6",
"sessionId": "d5cbcf77-b38b-4223-954f-c6a2fdc0c098",
"eventType": "PAGE_ATTRIBUTES",
"eventType": "PAGE",
"platform": "Web",
"timestamp": 1602485628504,
"domain": "test-browser.growingio.com",
"path": "/push/web.html",
"query": "a=1&b=2",
"title": "Hybrid测试页面",
"referralPage": "http://test-browser.growingio.com/push",
"protocolType": "https",
"globalSequenceId": 99,
"eventSequenceId": 3,
"pageShowTimestamp": 1602485626878,
"attributes": {
"key1": "value1",
"key2": "value2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
import com.gio.test.three.autotrack.R;
import com.gio.test.three.autotrack.fragments.GreenFragment;
import com.gio.test.three.autotrack.fragments.RedFragment;
import com.growingio.android.sdk.autotrack.GrowingAutotracker;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class HideFragmentActivity extends FragmentActivity {
private final Stack<Fragment> mAllFragments = new Stack<>();
private static final String TAG = "HideFragmentActivity";
private int i = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -76,6 +81,15 @@ public void onClick(View v) {
private Fragment createdFragment() {
if (mAllFragments.isEmpty()) {
Fragment fragment = new RedFragment();
if (i % 2 == 0) {
Map<String, String> map = new HashMap<>();
map.put("name", "HideFragmentActivity-RedFragment");
GrowingAutotracker.get().setPageAttributesSupport(fragment, map);
i++;
} else {
i++;
}
// GrowingAutotracker.get().setPageAttributesSupport(fragment, null);
mAllFragments.add(fragment);
return fragment;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.common.truth.Truth;
import com.growingio.android.sdk.autotrack.GrowingAutotracker;
import com.growingio.android.sdk.autotrack.IgnorePolicy;
import com.growingio.android.sdk.track.utils.JsonUtil;
import com.growingio.autotest.EventsTest;
import com.growingio.autotest.TestTrackConfiguration;
import com.growingio.autotest.help.Awaiter;
Expand Down Expand Up @@ -68,6 +69,12 @@
@LargeTest
public class PageEventsTest extends EventsTest {
private static final String TAG = "PageEventsTest";
private static final HashMap<String, String> TEST_ATTRIBUTES = new HashMap<String, String>() {{
put("key1", "value1");
put("key2", "value2");
put("key3", "");
put("key4", null);
}};

@BeforeAppOnCreate
public static void beforeAppOnCreate() {
Expand Down Expand Up @@ -256,6 +263,33 @@ protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
scenario.close();
}

@Test
public void setActivityPageAttributesTest() {
final AtomicBoolean receivedEvent = new AtomicBoolean(false);
getEventsApiServer().setOnReceivedEventListener(new MockEventsApiServer.OnReceivedEventListener() {
@Override
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
JSONObject jsonObject = jsonArray.getJSONObject(0);
if (jsonObject.getString("path").equals("/MainActivity")
&& jsonObject.getString("orientation").equals("PORTRAIT")
&& jsonObject.getString("title").equals("demos")
&& TEST_ATTRIBUTES.equals(JsonUtil.copyToMap(jsonObject.getJSONObject("attributes")))) {
receivedEvent.set(true);
}
}
});
ActivityLifecycleCallback activityLifecycleCallback = (activity, stage) -> {
if (stage == Stage.CREATED && activity.getClass() == MainActivity.class) {
GrowingAutotracker.get().setPageAttributes(activity, TEST_ATTRIBUTES);
}
};
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(activityLifecycleCallback);
ActivityScenario<MainActivity> scenario = ActivityScenario.launch(MainActivity.class);
Awaiter.untilTrue(receivedEvent);
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(activityLifecycleCallback);
scenario.close();
}

@Test
public void setFragmentPageAliasTest() {
final AtomicBoolean receivedEvent = new AtomicBoolean(false);
Expand Down Expand Up @@ -288,6 +322,37 @@ public void setFragmentPageAliasTest() {
scenario.close();
}

@Test
public void setFragmentPageAttributesTest() {
final AtomicBoolean receivedEvent = new AtomicBoolean(false);
getEventsApiServer().setOnReceivedEventListener(new MockEventsApiServer.OnReceivedEventListener() {
@Override
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String path = jsonObject.getString("path");
if ("/NestedFragmentActivity/GreenFragment[fragment1]".equals(path)) {
if (TEST_ATTRIBUTES.equals(JsonUtil.copyToMap(jsonObject.getJSONObject("attributes")))) {
receivedEvent.set(true);
}
}
}
}
});

FragmentLifecycleCallback fragmentLifecycleCallback = (fragment, stage) -> {
if (stage == FragmentLifecycleCallback.Stage.CREATED && fragment.getClass() == GreenFragment.class) {
GrowingAutotracker.get().setPageAttributesSupport(fragment, TEST_ATTRIBUTES);
}
};
FragmentLifecycleMonitor.get().addLifecycleCallback(fragmentLifecycleCallback);

ActivityScenario<NestedFragmentActivity> scenario = ActivityScenario.launch(NestedFragmentActivity.class);
Awaiter.untilTrue(receivedEvent);
FragmentLifecycleMonitor.get().removeLifecycleCallback(fragmentLifecycleCallback);
scenario.close();
}

@Test
public void fragmentOnHiddenChangedTest() {
Log.e(TAG, "fragmentOnHiddenChangedTest: Case 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
}

@Test
public void hybridPageEventWithQueryTest() {
public void hybridPageEventWithAttributesTest() {
AtomicBoolean receivedEvent = new AtomicBoolean(false);
WebView webView = launchMockWebView();
getEventsApiServer().setOnReceivedEventListener(new MockEventsApiServer.OnReceivedEventListener() {
Expand All @@ -279,23 +279,25 @@ protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
if (jsonObject.getString("path").equals("/push/web.html")) {
Truth.assertThat(jsonObject.getString("domain")).isEqualTo("test-browser.growingio.com");
Truth.assertThat(jsonObject.getString("title")).isEqualTo("Hybrid测试页面");
Truth.assertThat(jsonObject.getString("query")).isEqualTo("a=1&b=2");
Truth.assertThat(jsonObject.getString("referralPage")).isEqualTo("http://test-browser.growingio.com/push");
Truth.assertThat(jsonObject.getString("protocolType")).isEqualTo("https");
Truth.assertThat(jsonObject.getLong("timestamp")).isEqualTo(1602485628504L);
receivedEvent.set(true);
HashMap<String, String> attributes = (HashMap<String, String>) JsonUtil.copyToMap(jsonObject.getJSONObject("attributes"));
if (attributes.equals(TEST_ATTRIBUTES)) {
receivedEvent.set(true);
}
}
}
}
});

TrackHelper.postToUiThread(() ->
webView.evaluateJavascript("javascript:sendMockPageEventWithQuery()", null));
webView.evaluateJavascript("javascript:sendMockPageWithAttributesEvent()", null));
Awaiter.untilTrue(receivedEvent);
}

@Test
public void hybridFilePageEventTest() {
public void hybridPageEventWithQueryTest() {
AtomicBoolean receivedEvent = new AtomicBoolean(false);
WebView webView = launchMockWebView();
getEventsApiServer().setOnReceivedEventListener(new MockEventsApiServer.OnReceivedEventListener() {
Expand All @@ -304,9 +306,11 @@ protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.getString("path").equals("/push/web.html")) {
Truth.assertThat(jsonObject.getString("domain")).isEqualTo("com.gio.test.three");
Truth.assertThat(jsonObject.getString("domain")).isEqualTo("test-browser.growingio.com");
Truth.assertThat(jsonObject.getString("title")).isEqualTo("Hybrid测试页面");
Truth.assertThat(jsonObject.getString("protocolType")).isEqualTo("file");
Truth.assertThat(jsonObject.getString("query")).isEqualTo("a=1&b=2");
Truth.assertThat(jsonObject.getString("referralPage")).isEqualTo("http://test-browser.growingio.com/push");
Truth.assertThat(jsonObject.getString("protocolType")).isEqualTo("https");
Truth.assertThat(jsonObject.getLong("timestamp")).isEqualTo(1602485628504L);
receivedEvent.set(true);
}
Expand All @@ -315,32 +319,32 @@ protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
});

TrackHelper.postToUiThread(() ->
webView.evaluateJavascript("javascript:sendMockFilePageEvent()", null));
webView.evaluateJavascript("javascript:sendMockPageEventWithQuery()", null));
Awaiter.untilTrue(receivedEvent);
}

@Test
public void hybridPageAttributesEventTest() {
public void hybridFilePageEventTest() {
AtomicBoolean receivedEvent = new AtomicBoolean(false);
WebView webView = launchMockWebView();
getEventsApiServer().setOnReceivedEventListener(new MockEventsApiServer.OnReceivedEventListener() {
@Override
protected void onReceivedPageAttributesEvents(JSONArray jsonArray) throws JSONException {
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.getString("path").equals("/push/web.html")) {
Truth.assertThat(jsonObject.getString("domain")).isEqualTo("test-browser.growingio.com");
Truth.assertThat(jsonObject.getLong("pageShowTimestamp")).isEqualTo(1602485626878L);
HashMap<String, String> attributes = (HashMap<String, String>) JsonUtil.copyToMap(jsonObject.getJSONObject("attributes"));
Truth.assertThat(attributes).isEqualTo(TEST_ATTRIBUTES);
Truth.assertThat(jsonObject.getString("domain")).isEqualTo("com.gio.test.three");
Truth.assertThat(jsonObject.getString("title")).isEqualTo("Hybrid测试页面");
Truth.assertThat(jsonObject.getString("protocolType")).isEqualTo("file");
Truth.assertThat(jsonObject.getLong("timestamp")).isEqualTo(1602485628504L);
receivedEvent.set(true);
}
}
}
});

TrackHelper.postToUiThread(() ->
webView.evaluateJavascript("javascript:sendMockPageAttributesEvent()", null));
webView.evaluateJavascript("javascript:sendMockFilePageEvent()", null));
Awaiter.untilTrue(receivedEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ private void dispatchReceivedEvents(String json) {
mOnReceivedEventListener.onReceivedPageEvents(jsonArray);
}
break;
case AutotrackEventType.PAGE_ATTRIBUTES:
if (mOnReceivedEventListener != null) {
mOnReceivedEventListener.onReceivedPageAttributesEvents(jsonArray);
}
break;
case TrackEventType.FORM_SUBMIT:
if (mOnReceivedEventListener != null) {
mOnReceivedEventListener.onReceivedHybridFormSubmitEvents(jsonArray);
Expand Down Expand Up @@ -251,10 +246,6 @@ protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {

}

protected void onReceivedPageAttributesEvents(JSONArray jsonArray) throws JSONException {

}

protected void onReceivedHybridFormSubmitEvents(JSONArray jsonArray) throws JSONException {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,56 +102,41 @@ private void trackCustomEventX(String eventName, Map<String, String> attributes,

public void setPageAttributes(final Activity page, final Map<String, String> attributes) {
if (!isInited) return;
if (page == null || attributes == null || attributes.isEmpty()) {
if (page == null) {
Logger.e(TAG, "page or attributes is NULL");
return;
}
ThreadUtils.runOnUiThread(() -> PageProvider.get().setPageAttributes(page, new HashMap<>(attributes)));
ThreadUtils.runOnUiThread(() -> PageProvider.get().setPageAttributes(page, attributes == null ? new HashMap<>() : new HashMap<>(attributes)));
}

public void setPageAttributes(final android.app.Fragment page, final Map<String, String> attributes) {
if (!isInited) return;
if (page == null || attributes == null || attributes.isEmpty()) {
if (page == null) {
Logger.e(TAG, "page or attributes is NULL");
return;
}

ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
PageProvider.get().setPageAttributes(SuperFragment.make(page), new HashMap<>(attributes));
}
});
ThreadUtils.runOnUiThread(() -> PageProvider.get().setPageAttributes(SuperFragment.make(page), attributes == null ? new HashMap<>() : new HashMap<>(attributes)));
}

public void setPageAttributesSupport(final android.support.v4.app.Fragment page, final Map<String, String> attributes) {
if (!isInited) return;
if (page == null || attributes == null || attributes.isEmpty()) {
if (page == null) {
Logger.e(TAG, "page or attributes is NULL");
return;
}

ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
PageProvider.get().setPageAttributes(SuperFragment.makeSupport(page), new HashMap<>(attributes));
}
});
ThreadUtils.runOnUiThread(() -> PageProvider.get().setPageAttributes(SuperFragment.makeSupport(page), attributes == null ? new HashMap<>() : new HashMap<>(attributes)));
}

public void setPageAttributesX(final androidx.fragment.app.Fragment page, final Map<String, String> attributes) {
if (!isInited) return;
if (page == null || attributes == null || attributes.isEmpty()) {
if (page == null) {
Logger.e(TAG, "page or attributes is NULL");
return;
}

ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
PageProvider.get().setPageAttributes(SuperFragment.makeX(page), new HashMap<>(attributes));
}
});
ThreadUtils.runOnUiThread(() -> PageProvider.get().setPageAttributes(SuperFragment.makeX(page), attributes == null ? new HashMap<>() : new HashMap<>(attributes)));
}

public void trackViewImpression(View view, String impressionEventName) {
Expand Down
Loading

0 comments on commit 6d8d899

Please sign in to comment.