From 6d8d899c2da49194a63c700eed1e005bd93365ff Mon Sep 17 00:00:00 2001 From: styluo Date: Wed, 14 Dec 2022 18:57:42 +0800 Subject: [PATCH] feat:page attributes (#186) --- .../src/main/assets/autotrackHybridTest.html | 6 +- .../activity/HideFragmentActivity.java | 14 + .../autotracker/PageAttributesEventsTest.java | 347 ------------------ .../autotest/autotracker/PageEventsTest.java | 65 ++++ .../autotracker/hybrid/HybridEventsTest.java | 34 +- .../autotest/help/MockEventsApiServer.java | 9 - .../android/sdk/autotrack/Autotracker.java | 31 +- .../sdk/autotrack/page/PageProvider.java | 41 +-- .../protobuf/EventProtocolTransfer.java | 11 - .../com/growingio/protobuf/ProtocolTest.java | 22 -- .../android/hybrid/HybridTransformerImp.java | 12 +- .../growingio/android/hybrid/HybridTest.java | 6 - .../android/sdk/track/TrackMainThread.java | 4 - .../sdk/track/events/PageAttributesEvent.java | 90 ----- .../android/sdk/track/events/PageEvent.java | 13 +- .../hybrid/HybridPageAttributesEvent.java | 90 ----- .../sdk/track/TrackMainFilterTest.java | 19 - .../android/sdk/track/events/EventsTest.java | 11 - 18 files changed, 124 insertions(+), 701 deletions(-) delete mode 100644 demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageAttributesEventsTest.java delete mode 100644 growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageAttributesEvent.java delete mode 100644 growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/hybrid/HybridPageAttributesEvent.java diff --git a/demos/demo-autotrack/src/main/assets/autotrackHybridTest.html b/demos/demo-autotrack/src/main/assets/autotrackHybridTest.html index 6ba17bbf..f6ecb72b 100644 --- a/demos/demo-autotrack/src/main/assets/autotrackHybridTest.html +++ b/demos/demo-autotrack/src/main/assets/autotrackHybridTest.html @@ -191,11 +191,11 @@ 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", @@ -203,9 +203,9 @@ "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", diff --git a/demos/demo-autotrack/src/main/java/com/gio/test/three/autotrack/activity/HideFragmentActivity.java b/demos/demo-autotrack/src/main/java/com/gio/test/three/autotrack/activity/HideFragmentActivity.java index 0cfa9914..3c55e870 100644 --- a/demos/demo-autotrack/src/main/java/com/gio/test/three/autotrack/activity/HideFragmentActivity.java +++ b/demos/demo-autotrack/src/main/java/com/gio/test/three/autotrack/activity/HideFragmentActivity.java @@ -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 mAllFragments = new Stack<>(); + private static final String TAG = "HideFragmentActivity"; + private int i = 2; @Override protected void onCreate(Bundle savedInstanceState) { @@ -76,6 +81,15 @@ public void onClick(View v) { private Fragment createdFragment() { if (mAllFragments.isEmpty()) { Fragment fragment = new RedFragment(); + if (i % 2 == 0) { + Map 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; } diff --git a/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageAttributesEventsTest.java b/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageAttributesEventsTest.java deleted file mode 100644 index 743f4027..00000000 --- a/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageAttributesEventsTest.java +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.growingio.autotest.autotracker; - -import androidx.lifecycle.Lifecycle; -import androidx.test.core.app.ActivityScenario; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.LargeTest; -import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; -import androidx.test.runner.lifecycle.Stage; - -import com.gio.test.three.DemoApplication; -import com.gio.test.three.MainActivity; -import com.gio.test.three.autotrack.activity.NestedFragmentActivity; -import com.gio.test.three.autotrack.activity.TabFragmentActivity; -import com.gio.test.three.autotrack.activity.ui.main.PlaceholderFragment; -import com.gio.test.three.autotrack.fragments.FragmentLifecycleCallback; -import com.gio.test.three.autotrack.fragments.FragmentLifecycleMonitor; -import com.gio.test.three.autotrack.fragments.GreenFragment; -import com.google.common.truth.Truth; -import com.growingio.android.sdk.autotrack.GrowingAutotracker; -import com.growingio.android.sdk.track.utils.JsonUtil; -import com.growingio.autotest.EventsTest; -import com.growingio.autotest.TestTrackConfiguration; -import com.growingio.autotest.help.BeforeAppOnCreate; -import com.growingio.autotest.help.DataHelper; -import com.growingio.autotest.help.MockEventsApiServer; -import com.growingio.autotest.help.TrackHelper; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -@RunWith(AndroidJUnit4.class) -@LargeTest -public class PageAttributesEventsTest extends EventsTest { - private static final HashMap TEST_ATTRIBUTES = new HashMap() {{ - put("key1", "value1"); - put("key2", "value2"); - put("key3", ""); - put("key4", null); - }}; - - @BeforeAppOnCreate - public static void beforeAppOnCreate() { - DataHelper.deleteEventsDatabase(); - DemoApplication.setConfiguration(TestTrackConfiguration.getTestConfig()); - } - - @Test - public void beforeActivityOnResumePageAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/MainActivity", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback((activity, stage) -> { - if (activity.getClass() == MainActivity.class && stage == Stage.CREATED) { - GrowingAutotracker.get().setPageAttributes(activity, TEST_ATTRIBUTES); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(MainActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void afterActivityOnResumePageAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/MainActivity", TEST_ATTRIBUTES); - }}; - - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback((activity, stage) -> { - if (activity.getClass() == MainActivity.class && stage == Stage.RESUMED) { - TrackHelper.postToUiThread(() -> GrowingAutotracker.get().setPageAttributes(activity, TEST_ATTRIBUTES)); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(MainActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void beforeFragmentOnResumePageAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/TabFragmentActivity/PlaceholderFragment[android:switcher:view_pager:0]", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - FragmentLifecycleMonitor.get().addLifecycleCallback((fragment, stage) -> { - if (fragment.getClass() == PlaceholderFragment.class && stage == FragmentLifecycleCallback.Stage.CREATED) { - GrowingAutotracker.get().setPageAttributesSupport(fragment, TEST_ATTRIBUTES); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(TabFragmentActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void afterFragmentOnResumePageAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/TabFragmentActivity/PlaceholderFragment[android:switcher:view_pager:0]", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - FragmentLifecycleMonitor.get().addLifecycleCallback((fragment, stage) -> { - if (fragment.getClass() == PlaceholderFragment.class && stage == FragmentLifecycleCallback.Stage.RESUMED) { - TrackHelper.postToUiThread(() -> GrowingAutotracker.get().setPageAttributesSupport(fragment, TEST_ATTRIBUTES)); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(TabFragmentActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void beforeActivityOnResumeMultiPagesAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/NestedFragmentActivity", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/GreenFragment[fragment1]", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/GreenFragment[fragment1]/OrangeFragment[TestTag]", TEST_ATTRIBUTES); - put("*/GreenFragment[fragment1]/OrangeFragment[TestTag]/RedFragment[small]", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/RedFragment[fragment2]", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback((activity, stage) -> { - if (activity.getClass() == NestedFragmentActivity.class && stage == Stage.CREATED) { - GrowingAutotracker.get().setPageAttributes(activity, TEST_ATTRIBUTES); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(NestedFragmentActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void afterActivityOnResumeMultiPagesAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/NestedFragmentActivity", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/GreenFragment[fragment1]", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/GreenFragment[fragment1]/OrangeFragment[TestTag]", TEST_ATTRIBUTES); - put("*/GreenFragment[fragment1]/OrangeFragment[TestTag]/RedFragment[small]", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/RedFragment[fragment2]", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback((activity, stage) -> { - if (activity.getClass() == NestedFragmentActivity.class && stage == Stage.RESUMED) { - TrackHelper.postToUiThread(() -> GrowingAutotracker.get().setPageAttributes(activity, TEST_ATTRIBUTES)); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(NestedFragmentActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void beforeFragmentOnResumeMultiPagesAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/NestedFragmentActivity/GreenFragment[fragment1]", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/GreenFragment[fragment1]/OrangeFragment[TestTag]", TEST_ATTRIBUTES); - put("*/GreenFragment[fragment1]/OrangeFragment[TestTag]/RedFragment[small]", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - FragmentLifecycleMonitor.get().addLifecycleCallback((fragment, stage) -> { - if (fragment.getClass() == GreenFragment.class && stage == FragmentLifecycleCallback.Stage.CREATED) { - GrowingAutotracker.get().setPageAttributesSupport(fragment, TEST_ATTRIBUTES); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(NestedFragmentActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - @Test - public void afterFragmentOnResumeMultiPagesAttributesEventTest() { - HashMap> expectPagesAttributes = new HashMap>() {{ - put("/NestedFragmentActivity/GreenFragment[fragment1]", TEST_ATTRIBUTES); - put("/NestedFragmentActivity/GreenFragment[fragment1]/OrangeFragment[TestTag]", TEST_ATTRIBUTES); - put("*/GreenFragment[fragment1]/OrangeFragment[TestTag]/RedFragment[small]", TEST_ATTRIBUTES); - }}; - OnReceivedPageAttributesEventsListener eventsListener = new OnReceivedPageAttributesEventsListener(expectPagesAttributes); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - FragmentLifecycleMonitor.get().addLifecycleCallback((fragment, stage) -> { - if (fragment.getClass() == GreenFragment.class && stage == FragmentLifecycleCallback.Stage.RESUMED) { - TrackHelper.postToUiThread(() -> GrowingAutotracker.get().setPageAttributesSupport(fragment, TEST_ATTRIBUTES)); - } - }); - - ActivityScenario scenario = ActivityScenario.launch(NestedFragmentActivity.class); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - - eventsListener.reset(); - getEventsApiServer().setOnReceivedEventListener(eventsListener); - scenario.moveToState(Lifecycle.State.CREATED).moveToState(Lifecycle.State.RESUMED); - TrackHelper.waitForIdleSync(); - Truth.assertThat(eventsListener.checkEvents()).isTrue(); - scenario.close(); - } - - - private static final class OnReceivedPageAttributesEventsListener extends MockEventsApiServer.OnReceivedEventListener { - private static final class PagesAttributesParams { - public final long pageShowTimestamp; - public final HashMap attributes; - - PagesAttributesParams(long pageShowTimestamp, HashMap attributes) { - this.pageShowTimestamp = pageShowTimestamp; - this.attributes = attributes; - } - } - - - private final Map mReceivedPages = new HashMap<>(); - private final Map mReceivedPagesAttributes = new ConcurrentHashMap<>(); - private final HashMap> mExpectPagesAttributes; - - OnReceivedPageAttributesEventsListener(HashMap> expectPagesAttributes) { - mExpectPagesAttributes = new HashMap<>(expectPagesAttributes); - } - - @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"); - mReceivedPages.put(path, jsonObject.getLong("timestamp")); - } - } - - @Override - protected void onReceivedPageAttributesEvents(JSONArray jsonArray) throws JSONException { - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject jsonObject = jsonArray.getJSONObject(i); - String path = jsonObject.getString("path"); - long pageShowTimestamp = jsonObject.getLong("pageShowTimestamp"); - HashMap attributes = (HashMap) JsonUtil.copyToMap(jsonObject.getJSONObject("attributes")); - mReceivedPagesAttributes.put(path, new PagesAttributesParams(pageShowTimestamp, attributes)); - } - } - - public void reset() { - mReceivedPages.clear(); - mReceivedPagesAttributes.clear(); - } - - public boolean checkEvents() { - for (Map.Entry> entry : mExpectPagesAttributes.entrySet()) { - String path = entry.getKey(); - PagesAttributesParams receivedAttributesParams = mReceivedPagesAttributes.get(path); - long expectPageTimestamp = mReceivedPages.get(path); - long actualPageTimestamp = receivedAttributesParams.pageShowTimestamp; - if (actualPageTimestamp != expectPageTimestamp) { - Truth.assertWithMessage(path + " Page show timestamp is " + expectPageTimestamp - + ", but PageAttributesEvent pageShowTimestamp is " + actualPageTimestamp).fail(); - } - HashMap expectAttributes = entry.getValue(); - if (!expectAttributes.equals(receivedAttributesParams.attributes)) { - Truth.assertWithMessage(path + " Page attributes is " + receivedAttributesParams.attributes + "not equals expect attributes " + expectAttributes).fail(); - } - } - return true; - } - } -} diff --git a/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageEventsTest.java b/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageEventsTest.java index 4ca4961c..51022fcc 100644 --- a/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageEventsTest.java +++ b/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/PageEventsTest.java @@ -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; @@ -68,6 +69,12 @@ @LargeTest public class PageEventsTest extends EventsTest { private static final String TAG = "PageEventsTest"; + private static final HashMap TEST_ATTRIBUTES = new HashMap() {{ + put("key1", "value1"); + put("key2", "value2"); + put("key3", ""); + put("key4", null); + }}; @BeforeAppOnCreate public static void beforeAppOnCreate() { @@ -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 scenario = ActivityScenario.launch(MainActivity.class); + Awaiter.untilTrue(receivedEvent); + ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(activityLifecycleCallback); + scenario.close(); + } + @Test public void setFragmentPageAliasTest() { final AtomicBoolean receivedEvent = new AtomicBoolean(false); @@ -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 scenario = ActivityScenario.launch(NestedFragmentActivity.class); + Awaiter.untilTrue(receivedEvent); + FragmentLifecycleMonitor.get().removeLifecycleCallback(fragmentLifecycleCallback); + scenario.close(); + } + @Test public void fragmentOnHiddenChangedTest() { Log.e(TAG, "fragmentOnHiddenChangedTest: Case 1"); diff --git a/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/hybrid/HybridEventsTest.java b/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/hybrid/HybridEventsTest.java index 5cf431f8..4dea77b0 100644 --- a/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/hybrid/HybridEventsTest.java +++ b/demos/demo/src/androidTest/java/com/growingio/autotest/autotracker/hybrid/HybridEventsTest.java @@ -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() { @@ -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 attributes = (HashMap) 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() { @@ -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); } @@ -315,24 +319,24 @@ 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 attributes = (HashMap) 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); } } @@ -340,7 +344,7 @@ protected void onReceivedPageAttributesEvents(JSONArray jsonArray) throws JSONEx }); TrackHelper.postToUiThread(() -> - webView.evaluateJavascript("javascript:sendMockPageAttributesEvent()", null)); + webView.evaluateJavascript("javascript:sendMockFilePageEvent()", null)); Awaiter.untilTrue(receivedEvent); } diff --git a/demos/demo/src/androidTest/java/com/growingio/autotest/help/MockEventsApiServer.java b/demos/demo/src/androidTest/java/com/growingio/autotest/help/MockEventsApiServer.java index 5ced8030..9826b0c7 100644 --- a/demos/demo/src/androidTest/java/com/growingio/autotest/help/MockEventsApiServer.java +++ b/demos/demo/src/androidTest/java/com/growingio/autotest/help/MockEventsApiServer.java @@ -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); @@ -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 { } diff --git a/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/Autotracker.java b/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/Autotracker.java index 9b1a8743..d707e7f4 100644 --- a/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/Autotracker.java +++ b/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/Autotracker.java @@ -102,56 +102,41 @@ private void trackCustomEventX(String eventName, Map attributes, public void setPageAttributes(final Activity page, final Map 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 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 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 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) { diff --git a/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/page/PageProvider.java b/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/page/PageProvider.java index fa344b1d..43c9b4b9 100644 --- a/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/page/PageProvider.java +++ b/growingio-autotracker-core/src/main/java/com/growingio/android/sdk/autotrack/page/PageProvider.java @@ -25,7 +25,6 @@ import com.growingio.android.sdk.TrackerContext; import com.growingio.android.sdk.autotrack.IgnorePolicy; -import com.growingio.android.sdk.track.events.PageAttributesEvent; import com.growingio.android.sdk.track.events.PageEvent; import com.growingio.android.sdk.autotrack.view.ViewAttributeUtil; import com.growingio.android.sdk.track.TrackMainThread; @@ -125,8 +124,8 @@ private void sendPage(Context context, Page page) { } if (!page.isIgnored()) { Logger.d(TAG, "sendPage: path = " + page.path()); + useCachePageAttributesIfNeeded(page); generatePageEvent(context, page); - reissuePageAttributes(page); } else { Logger.e(TAG, "sendPage: path = " + page.path() + " is ignored"); } @@ -141,6 +140,7 @@ private void generatePageEvent(Context context, Page page) { .setTitle(page.getTitle()) .setTimestamp(page.getShowTimestamp()) .setOrientation(orientation) + .setAttributes(page.getAttributes()) ); } @@ -384,7 +384,7 @@ private Page findPage(SuperFragment carrier, Page page) { return null; } - private void reissuePageAttributes(Page page) { + private void useCachePageAttributesIfNeeded(Page page) { Map attributes = null; if (page.getCarrier() instanceof Activity) { attributes = PAGE_ATTRIBUTES_CACHE.remove(page.getCarrier()); @@ -394,20 +394,6 @@ private void reissuePageAttributes(Page page) { if (attributes != null) { setPageAttributes(page, attributes, false); - return; - } - - attributes = page.getAttributes(); - if (attributes != null) { - setPageAttributes(page, attributes, false); - return; - } - - if (page.getParent() != null) { - attributes = page.getParent().getAttributes(); - if (attributes != null) { - setPageAttributes(page, attributes, false); - } } } @@ -438,27 +424,6 @@ private void setPageAttributes(Page page, Map attributes, boo } page.setAttributes(attributes); - - if (!page.isIgnored()) { - Logger.d(TAG, "setPageAttributes: page = " + page.path() + ", attributes = " + attributes.toString()); - generatePageAttributesEvent(page); - } - - if (!page.getAllChildren().isEmpty()) { - for (Page child : page.getAllChildren()) { - if (child.getShowTimestamp() >= page.getShowTimestamp()) { - setPageAttributes(child, attributes, checkEquals); - } - } - } - } - - private void generatePageAttributesEvent(Page page) { - TrackMainThread.trackMain().postEventToTrackMain( - new PageAttributesEvent.Builder() - .setPath(page.path()) - .setPageShowTimestamp(page.getShowTimestamp()) - .setAttributes(page.getAttributes())); } public Page findPage(Activity activity) { diff --git a/growingio-data/protobuf/src/main/java/com/growingio/protobuf/EventProtocolTransfer.java b/growingio-data/protobuf/src/main/java/com/growingio/protobuf/EventProtocolTransfer.java index 20d6d895..19590e27 100644 --- a/growingio-data/protobuf/src/main/java/com/growingio/protobuf/EventProtocolTransfer.java +++ b/growingio-data/protobuf/src/main/java/com/growingio/protobuf/EventProtocolTransfer.java @@ -19,7 +19,6 @@ import com.growingio.android.advert.ActivateEvent; import com.growingio.android.sdk.track.events.AutotrackEventType; import com.growingio.android.sdk.track.events.CustomEvent; -import com.growingio.android.sdk.track.events.PageAttributesEvent; import com.growingio.android.sdk.track.events.PageEvent; import com.growingio.android.sdk.track.events.PageLevelCustomEvent; import com.growingio.android.sdk.track.events.TrackEventType; @@ -30,7 +29,6 @@ import com.growingio.android.sdk.track.events.cdp.ResourceItem; import com.growingio.android.sdk.track.events.cdp.ResourceItemCustomEvent; import com.growingio.android.sdk.track.events.hybrid.HybridCustomEvent; -import com.growingio.android.sdk.track.events.hybrid.HybridPageAttributesEvent; import com.growingio.android.sdk.track.events.hybrid.HybridPageEvent; import com.growingio.android.sdk.track.events.hybrid.HybridViewElementEvent; import com.growingio.android.sdk.track.middleware.GEvent; @@ -136,11 +134,6 @@ public static byte[] protocol(GEvent gEvent) { CustomEvent customEvent = (CustomEvent) gEvent; eventBuilder.setEventName(customEvent.getEventName()); //22 } - if (gEvent instanceof PageAttributesEvent) { - PageAttributesEvent paEvent = (PageAttributesEvent) gEvent; - eventBuilder.setPath(paEvent.getPath()); //10 - eventBuilder.setPageShowTimestamp(paEvent.getPageShowTimestamp()); //23 - } if (gEvent instanceof PageLevelCustomEvent) { PageLevelCustomEvent plEvent = (PageLevelCustomEvent) gEvent; eventBuilder.setPath(plEvent.getPath()); //10 @@ -170,10 +163,6 @@ public static byte[] protocol(GEvent gEvent) { HybridCustomEvent hcEvent = (HybridCustomEvent) gEvent; eventBuilder.setQuery(hcEvent.getQuery()); //11 } - if (gEvent instanceof HybridPageAttributesEvent) { - HybridPageAttributesEvent hpaEvent = (HybridPageAttributesEvent) gEvent; - eventBuilder.setQuery(hpaEvent.getQuery()); //11 - } if (gEvent instanceof HybridPageEvent) { HybridPageEvent hpEvent = (HybridPageEvent) gEvent; eventBuilder.setQuery(hpEvent.getQuery()); //11 diff --git a/growingio-data/protobuf/src/test/java/com/growingio/protobuf/ProtocolTest.java b/growingio-data/protobuf/src/test/java/com/growingio/protobuf/ProtocolTest.java index d667ab2d..15ab8cf0 100644 --- a/growingio-data/protobuf/src/test/java/com/growingio/protobuf/ProtocolTest.java +++ b/growingio-data/protobuf/src/test/java/com/growingio/protobuf/ProtocolTest.java @@ -27,7 +27,6 @@ import com.growingio.android.sdk.track.events.ConversionVariablesEvent; import com.growingio.android.sdk.track.events.CustomEvent; import com.growingio.android.sdk.track.events.LoginUserAttributesEvent; -import com.growingio.android.sdk.track.events.PageAttributesEvent; import com.growingio.android.sdk.track.events.PageEvent; import com.growingio.android.sdk.track.events.PageLevelCustomEvent; import com.growingio.android.sdk.track.events.ViewElementEvent; @@ -35,7 +34,6 @@ import com.growingio.android.sdk.track.events.cdp.ResourceItem; import com.growingio.android.sdk.track.events.cdp.ResourceItemCustomEvent; import com.growingio.android.sdk.track.events.hybrid.HybridCustomEvent; -import com.growingio.android.sdk.track.events.hybrid.HybridPageAttributesEvent; import com.growingio.android.sdk.track.events.hybrid.HybridPageEvent; import com.growingio.android.sdk.track.events.hybrid.HybridViewElementEvent; import com.growingio.android.sdk.track.middleware.GEvent; @@ -79,15 +77,6 @@ public void protocolTest() throws InvalidProtocolBufferException { Truth.assertThat(protocol(pageEvent).getReferralPage()).isEqualTo("test"); Truth.assertThat(protocol(pageEvent).getOrientation()).isEqualTo("vertical"); - PageAttributesEvent paEvent = new PageAttributesEvent.Builder() - .setPath("database") - .setPageShowTimestamp(1000L) - .setAttributes(defaultMap) - .build(); - Truth.assertThat(protocol(paEvent).getPath()).isEqualTo("database"); - Truth.assertThat(protocol(paEvent).getPageShowTimestamp()).isEqualTo(1000L); - Truth.assertThat(protocol(paEvent).getAttributesOrDefault("user", "gio")).isEqualTo("cpacm"); - CustomEvent customEvent = new CustomEvent.Builder() .setEventName("databaseTest") .build(); @@ -117,17 +106,6 @@ public void protocolTest() throws InvalidProtocolBufferException { Truth.assertThat(protocol(hcEvent).getAttributesOrDefault("user", "gio")).isEqualTo("cpacm"); Truth.assertThat(protocol(hcEvent).getEventName()).isEqualTo("hybrid"); - HybridPageAttributesEvent hpaEvent = new HybridPageAttributesEvent.Builder() - .setDomain("growingio.com") - .setQuery("aaaa") - .setAttributes(defaultMap) - .setPath("test") - .build(); - Truth.assertThat(protocol(hpaEvent).getAttributesOrDefault("user", "gio")).isEqualTo("cpacm"); - Truth.assertThat(protocol(hpaEvent).getQuery()).isEqualTo("aaaa"); - Truth.assertThat(protocol(hpaEvent).getDomain()).isEqualTo("growingio.com"); - Truth.assertThat(protocol(hpaEvent).getPath()).isEqualTo("test"); - HybridPageEvent hpEvent = new HybridPageEvent.Builder() .setProtocolType("protobuf") .build(); diff --git a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/HybridTransformerImp.java b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/HybridTransformerImp.java index f4ec953e..72767909 100644 --- a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/HybridTransformerImp.java +++ b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/HybridTransformerImp.java @@ -21,7 +21,6 @@ import android.text.TextUtils; import com.growingio.android.sdk.track.events.hybrid.HybridCustomEvent; -import com.growingio.android.sdk.track.events.hybrid.HybridPageAttributesEvent; import com.growingio.android.sdk.track.events.hybrid.HybridPageEvent; import com.growingio.android.sdk.track.events.hybrid.HybridViewElementEvent; import com.growingio.android.sdk.track.events.AutotrackEventType; @@ -78,15 +77,8 @@ public BaseEvent.BaseBuilder transform(String hybridEvent) { .setReferralPage(eventJson.optString(KEY_REFERRAL_PAGE)) .setTitle(eventJson.optString(KEY_TITLE)) .setTimestamp(eventJson.getLong(KEY_TIMESTAMP)) - .setOrientation(orientation); - - } else if (AutotrackEventType.PAGE_ATTRIBUTES.equals(type)) { - return new HybridPageAttributesEvent.Builder() - .setDomain(getDomain(eventJson)) - .setQuery(eventJson.optString(KEY_QUERY)) - .setPath(eventJson.getString(KEY_PATH)) - .setPageShowTimestamp(eventJson.getLong(KEY_PAGE_SHOW_TIMESTAMP)) - .setAttributes(JsonUtil.copyToMap(eventJson.getJSONObject(KEY_ATTRIBUTES))); + .setOrientation(orientation) + .setAttributes(JsonUtil.copyToMap(eventJson.optJSONObject(KEY_ATTRIBUTES))); } else if (AutotrackEventType.VIEW_CLICK.equals(type)) { return transformViewElementEventBuilder(type, eventJson); diff --git a/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java b/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java index 88808194..5a80c481 100644 --- a/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java +++ b/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java @@ -28,7 +28,6 @@ import com.growingio.android.sdk.CoreConfiguration; import com.growingio.android.sdk.TrackerContext; import com.growingio.android.sdk.track.events.hybrid.HybridCustomEvent; -import com.growingio.android.sdk.track.events.hybrid.HybridPageAttributesEvent; import com.growingio.android.sdk.track.events.hybrid.HybridPageEvent; import com.growingio.android.sdk.track.events.hybrid.HybridViewElementEvent; import com.growingio.android.sdk.track.async.Callback; @@ -106,11 +105,6 @@ public void transformTest() { Truth.assertThat(viewClickEvent.getIndex()).isEqualTo(1); Truth.assertThat(viewClickEvent.getPath()).isEqualTo("urlscheme"); - String pageAttrJson = "{\"eventType\":\"PAGE_ATTRIBUTES\",\"domain\":\"growingio.com\",\"query\":\"something\",\"index\":1,\"textValue\":\"test\",\"path\":\"urlscheme\",\"pageShowTimestamp\":1234567890,\"attributes\":{\"name\":\"cpacm\",\"age\":\"18\"}}"; - HybridPageAttributesEvent pageAttrEvent = (HybridPageAttributesEvent) hybridTransformerImp.transform(pageAttrJson).build(); - Truth.assertThat(pageAttrEvent.getQuery()).isEqualTo("something"); - Truth.assertThat(pageAttrEvent.toJSONObject().toString()).contains("growingio.com"); - String pageJson = "{\"eventType\":\"PAGE\",\"domain\":\"growingio.com\",\"protocolType\":\"webview\",\"query\":\"something\",\"index\":1,\"textValue\":\"test\",\"path\":\"urlscheme\",\"referralPage\":\"/home/product/\",\"timestamp\":123456789,\"title\":\"this is a test page.\"}"; HybridPageEvent pageEvent = (HybridPageEvent) hybridTransformerImp.transform(pageJson).build(); Truth.assertThat(pageEvent.getProtocolType()).isEqualTo("webview"); diff --git a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/TrackMainThread.java b/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/TrackMainThread.java index 8aaf1ee8..020d0d05 100644 --- a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/TrackMainThread.java +++ b/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/TrackMainThread.java @@ -28,7 +28,6 @@ import com.growingio.android.sdk.CoreConfiguration; import com.growingio.android.sdk.track.events.CustomEvent; import com.growingio.android.sdk.track.events.EventFilterInterceptor; -import com.growingio.android.sdk.track.events.PageAttributesEvent; import com.growingio.android.sdk.track.events.PageEvent; import com.growingio.android.sdk.track.events.PageLevelCustomEvent; import com.growingio.android.sdk.track.events.ViewElementEvent; @@ -233,9 +232,6 @@ String getEventPath(BaseEvent.BaseBuilder eventBuilder) { if (eventBuilder instanceof PageLevelCustomEvent.Builder) { return ((PageLevelCustomEvent.Builder) eventBuilder).getPath(); } - if (eventBuilder instanceof PageAttributesEvent.Builder) { - return ((PageAttributesEvent.Builder) eventBuilder).getPath(); - } return null; } diff --git a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageAttributesEvent.java b/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageAttributesEvent.java deleted file mode 100644 index 88d71fa5..00000000 --- a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageAttributesEvent.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.growingio.android.sdk.track.events; - -import com.growingio.android.sdk.track.events.base.BaseAttributesEvent; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Map; - -public class PageAttributesEvent extends BaseAttributesEvent { - private static final long serialVersionUID = 1L; - - private final String mPath; - private final long mPageShowTimestamp; - - protected PageAttributesEvent(Builder eventBuilder) { - super(eventBuilder); - mPath = eventBuilder.mPath; - mPageShowTimestamp = eventBuilder.mPageShowTimestamp; - } - - public String getPath() { - return checkValueSafe(mPath); - } - - public long getPageShowTimestamp() { - return mPageShowTimestamp; - } - - @Override - public JSONObject toJSONObject() { - JSONObject json = super.toJSONObject(); - try { - json.put("path", getPath()); - json.put("pageShowTimestamp", getPageShowTimestamp()); - } catch (JSONException ignored) { - } - return json; - } - - public static class Builder extends BaseAttributesEvent.Builder { - private String mPath; - private long mPageShowTimestamp; - - public Builder() { - super(AutotrackEventType.PAGE_ATTRIBUTES); - } - - public Builder setPath(String path) { - mPath = path; - return this; - } - - public String getPath() { - return mPath; - } - - public Builder setPageShowTimestamp(long pageShowTimestamp) { - mPageShowTimestamp = pageShowTimestamp; - return this; - } - - @Override - public Builder setAttributes(Map attributes) { - super.setAttributes(attributes); - return this; - } - - @Override - public PageAttributesEvent build() { - return new PageAttributesEvent(this); - } - } -} diff --git a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageEvent.java b/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageEvent.java index 6502cfe6..03c19c6e 100644 --- a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageEvent.java +++ b/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/PageEvent.java @@ -18,15 +18,16 @@ import androidx.annotation.StringDef; -import com.growingio.android.sdk.track.events.base.BaseEvent; +import com.growingio.android.sdk.track.events.base.BaseAttributesEvent; import org.json.JSONException; import org.json.JSONObject; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.Map; -public class PageEvent extends BaseEvent { +public class PageEvent extends BaseAttributesEvent { private static final long serialVersionUID = 1L; public static final String ORIENTATION_PORTRAIT = "PORTRAIT"; @@ -79,7 +80,7 @@ public JSONObject toJSONObject() { return json; } - public static class Builder extends BaseBuilder { + public static class Builder extends BaseAttributesEvent.Builder { private String mPath; private String mOrientation = ORIENTATION_PORTRAIT; private String mTitle; @@ -127,5 +128,11 @@ public Builder setTimestamp(long timestamp) { mTimestamp = timestamp; return this; } + + @Override + public Builder setAttributes(Map attributes) { + super.setAttributes(attributes); + return this; + } } } diff --git a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/hybrid/HybridPageAttributesEvent.java b/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/hybrid/HybridPageAttributesEvent.java deleted file mode 100644 index c464b8cd..00000000 --- a/growingio-tracker-core/src/main/java/com/growingio/android/sdk/track/events/hybrid/HybridPageAttributesEvent.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.growingio.android.sdk.track.events.hybrid; - -import com.growingio.android.sdk.track.events.PageAttributesEvent; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Map; - -public final class HybridPageAttributesEvent extends PageAttributesEvent { - private static final long serialVersionUID = 1L; - - private final String mQuery; - - protected HybridPageAttributesEvent(Builder eventBuilder) { - super(eventBuilder); - mQuery = eventBuilder.mQuery; - } - - public String getQuery() { - return checkValueSafe(mQuery); - } - - @Override - public JSONObject toJSONObject() { - JSONObject json = super.toJSONObject(); - try { - json.put("query", mQuery); - } catch (JSONException ignored) { - } - return json; - } - - public static class Builder extends PageAttributesEvent.Builder { - private String mQuery; - - public Builder() { - super(); - } - - public Builder setQuery(String query) { - mQuery = query; - return this; - } - - public Builder setDomain(String domain) { - mDomain = domain; - return this; - } - - @Override - public HybridPageAttributesEvent build() { - return new HybridPageAttributesEvent(this); - } - - @Override - public Builder setPath(String path) { - super.setPath(path); - return this; - } - - @Override - public Builder setPageShowTimestamp(long pageShowTimestamp) { - super.setPageShowTimestamp(pageShowTimestamp); - return this; - } - - @Override - public Builder setAttributes(Map attributes) { - super.setAttributes(attributes); - return this; - } - } -} diff --git a/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/TrackMainFilterTest.java b/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/TrackMainFilterTest.java index e390add8..b5dca9ce 100644 --- a/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/TrackMainFilterTest.java +++ b/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/TrackMainFilterTest.java @@ -25,7 +25,6 @@ import com.growingio.android.sdk.TrackerContext; import com.growingio.android.sdk.track.events.CustomEvent; import com.growingio.android.sdk.track.events.EventBuildInterceptor; -import com.growingio.android.sdk.track.events.PageAttributesEvent; import com.growingio.android.sdk.track.events.PageEvent; import com.growingio.android.sdk.track.events.TrackEventType; import com.growingio.android.sdk.track.events.base.BaseEvent; @@ -108,24 +107,6 @@ public void eventDidBuild(GEvent event) { trackMainThread.onGenerateGEvent(new CustomEvent.Builder().setEventName("cpacm")); } - @Test - public void filterEventPath() { - trackMainThread.addEventBuildInterceptor(new EventBuildInterceptor() { - @Override - public void eventWillBuild(BaseEvent.BaseBuilder eventBuilder) { - } - - @Override - public void eventDidBuild(GEvent event) { - if (event instanceof PageAttributesEvent) { - Truth.assertThat(((PageAttributesEvent) event).getPath()).isEqualTo("/SubActivity"); - } - } - }); - trackMainThread.onGenerateGEvent(new PageAttributesEvent.Builder().setPath("/MainActivity")); - trackMainThread.onGenerateGEvent(new PageAttributesEvent.Builder().setPath("/SubActivity")); - } - @Test public void filterEventName() { trackMainThread.addEventBuildInterceptor(new EventBuildInterceptor() { diff --git a/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/events/EventsTest.java b/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/events/EventsTest.java index b682f60c..c0aab149 100644 --- a/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/events/EventsTest.java +++ b/growingio-tracker-core/src/test/java/com/growingio/android/sdk/track/events/EventsTest.java @@ -139,17 +139,6 @@ public void eventLoginUserBuilder() { Truth.assertThat("||1").isEqualTo(map.get("key10")); } - @Test - public void eventPageAttr() { - PageAttributesEvent event = new PageAttributesEvent.Builder() - .setPath("/blank") - .setPageShowTimestamp(System.currentTimeMillis()) - .setAttributes(new HashMap<>()) - .build(); - Truth.assertThat(event.getEventType()).isEqualTo(AutotrackEventType.PAGE_ATTRIBUTES); - inRobolectric(event.toJSONObject()); - } - @Test public void eventPage() { PageEvent event = new PageEvent.Builder()