Skip to content

Commit

Permalink
add path in custom event for haier
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacm committed Sep 27, 2024
1 parent fb41b52 commit 6d5c6be
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 12 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ subprojects {
kotlin {
target '**/*.kt'
targetExclude("$buildDir/**/*.kt")

ktlint()
ktlint().editorConfigOverride([
"ktlint_standard_comment-wrapping": "disabled",
"max_line_length" : 2147483647,
])
}
groovyGradle {
target '*.gradle'
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ navigation = "2.5.3"
pluginGradle = "7.4.2" # 4.2.2-7.4.2
kotlin = "1.9.21"

spotless = "6.20.0"
spotless = "6.25.0"

# https://developer.android.com/jetpack/androidx/releases/compose-compiler
# composeBom = "2023.04.00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ internal class JsonFactoryGenerator(
ClassName.get(
ProcessUtils.JSON_SERIALIZABLE_PACKAGE,
ProcessUtils.JSON_SERIALIZABLE_CLASS,
), // rawType
ClassName.get(eventType), // the value for T
builderType, // the value for R
),
ClassName.get(eventType),
builderType,
)

val jonSerialBuilder = TypeSpec.classBuilder(generateClass).addJavadoc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ internal class JsonSerializerGenerator(
val builderType = ClassName.get(eventBuilderType)

val superinterface = ParameterizedTypeName.get(
ClassName.get(JSON_SERIALIZABLE_PACKAGE, JSON_SERIALIZABLE_CLASS), // rawType
ClassName.get(eventType), // the value for T
builderType, // the value for R
ClassName.get(JSON_SERIALIZABLE_PACKAGE, JSON_SERIALIZABLE_CLASS),
ClassName.get(eventType),
builderType,
)

val jonSerialBuilder = TypeSpec.classBuilder(generateClass).addJavadoc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class AutotrackConfig implements Configurable {
private float mImpressionScale = 0;
private boolean enableFragmentTag = false;
private boolean customEventWithPage = true;
private final AutotrackOptions mAutotrackOptions = new AutotrackOptions();

private boolean autotrackEnabled = true;
Expand Down Expand Up @@ -154,4 +155,16 @@ public AutotrackConfig setAutotrack(boolean enabled) {
this.autotrackEnabled = enabled;
return this;
}

/**
* Refer the custom event with the page. If called, the custom event will be associated with the page path.
*/
public AutotrackConfig referCustomEventWithPage() {
this.customEventWithPage = true;
return this;
}

public boolean isReferCustomEventWithPage() {
return this.customEventWithPage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import android.view.View;
import android.widget.EditText;

import com.growingio.android.sdk.TrackerContext;
import com.growingio.android.sdk.autotrack.inject.InjectorProvider;
import com.growingio.android.sdk.autotrack.impression.ImpressionProvider;
import com.growingio.android.sdk.autotrack.page.Page;
import com.growingio.android.sdk.autotrack.page.PageProvider;
import com.growingio.android.sdk.autotrack.page.SuperFragment;
import com.growingio.android.sdk.autotrack.view.ViewAttributeUtil;
import com.growingio.android.sdk.Tracker;
import com.growingio.android.sdk.autotrack.view.ViewNodeProvider;
import com.growingio.android.sdk.track.TrackMainThread;
import com.growingio.android.sdk.track.events.TrackEventGenerator;
import com.growingio.android.sdk.track.log.Logger;
import com.growingio.android.sdk.track.providers.TrackerLifecycleProvider;

Expand All @@ -55,6 +58,46 @@ protected Map<Class<? extends TrackerLifecycleProvider>, TrackerLifecycleProvide
return providerMap;
}


@Override
public void trackCustomEvent(String eventName) {
if (!isInited) return;
if (TextUtils.isEmpty(eventName)) {
Logger.e(TAG, "trackCustomEvent: eventName is NULL");
return;
}
trackCustomEvent(eventName, null);
}

@Override
public void trackCustomEvent(String eventName, Map<String, String> attributes) {
if (!isInited) return;
if (TextUtils.isEmpty(eventName)) {
Logger.e(TAG, "trackCustomEvent: eventName is NULL");
return;
}
TrackMainThread.trackMain().runOnUiThread(() -> {
TrackerContext trackerContext = getContext();
if (trackerContext == null) {
return;
}
AutotrackConfig config = trackerContext.getConfigurationProvider().getConfiguration(AutotrackConfig.class);
if (config == null || !config.isReferCustomEventWithPage()) {
super.trackCustomEvent(eventName, attributes);
return;
}
Page<?> lastPage = PageProvider.get().findLatestPage();
if (lastPage == null) {
super.trackCustomEvent(eventName, attributes);
return;
}
String path = lastPage.activePath();
if (path.isEmpty()) path = lastPage.path();
TrackEventGenerator.generatePageCustomEvent(eventName, attributes, path);
});
}


/**
* Generate an event for the current page
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,21 @@ public Page<?> findPage(View view) {
}
return null;
}

public Page<?> findLatestPage(){
if (ALL_PAGE_TREE.isEmpty()) {
return null;
}
Page<?> page = null;
for (ActivityPage activityPage : ALL_PAGE_TREE.values()) {
if (page == null) {
page = activityPage;
} else {
if (activityPage.getShowTimestamp() > page.getShowTimestamp()) {
page = activityPage;
}
}
}
return page;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ public void setup() {
Application application = ApplicationProvider.getApplicationContext();

Map<Class<? extends Configurable>, Configurable> modules = new HashMap<>();
modules.put(AutotrackConfig.class, new AutotrackConfig().setImpressionScale(0.5f));
modules.put(AutotrackConfig.class,
new AutotrackConfig().setImpressionScale(0.5f)
.setWebViewBridgeEnabled(true)
.setPageRuleXml(0)
.addPageRule("MainActivity", "com.growingio.android.sdk.autotrack.RobolectricActivity")
.addPageMatchRule(".*")
.enableFragmentTag(false)

);
TrackerLifecycleProviderFactory.create().createConfigurationProviderWithConfig(new CoreConfiguration("AutotrackTest", "growingio://autotrack"), modules);
autotracker = new Autotracker(application);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ private static EventV3Protocol.EventV3Dto protocol(GEvent gEvent) {
if (gEvent instanceof PageLevelCustomEvent) {
PageLevelCustomEvent plEvent = (PageLevelCustomEvent) gEvent;
eventBuilder.setPath(plEvent.getPath()); //10
eventBuilder.setPageShowTimestamp(plEvent.getPageShowTimestamp()); //23
if (plEvent.getPageShowTimestamp() > 0) {
eventBuilder.setPageShowTimestamp(plEvent.getPageShowTimestamp());//23
}
}
if (gEvent instanceof VisitEvent) {
VisitEvent visitEvent = (VisitEvent) gEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void trackCustomEvent(String eventName, Map<String, String> attributes) {
if (attributes != null) {
attributes = new HashMap<>(attributes);
}
TrackEventGenerator.generateCustomEvent(eventName, attributes);
TrackEventGenerator.generatePageCustomEvent(eventName, attributes,"/fake");
}

public void setDynamicGeneralPropsGenerator(DynamicGeneralPropsGenerator generator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public static void generateCustomEvent(String name, Map<String, String> attribut
);
}

public static void generatePageCustomEvent(String name, Map<String, String> attributes, String path) {
TrackMainThread.trackMain().postEventToTrackMain(
new PageLevelCustomEvent.Builder()
.setPath(path)
.setEventName(name)
.setAttributes(attributes)
);
}

public static void generateConversionVariablesEvent(Map<String, String> variables) {
TrackMainThread.trackMain().postEventToTrackMain(
new ConversionVariablesEvent.Builder()
Expand Down

0 comments on commit 6d5c6be

Please sign in to comment.