Skip to content

Commit

Permalink
feat: custom event refer with page. (#221)
Browse files Browse the repository at this point in the history
* add path in custom event for haier

* GIOFakePage to "/"

* exclude hybrid custom event.

* custom event with page default is false.
  • Loading branch information
cpacm authored Sep 30, 2024
1 parent fb41b52 commit 977ae04
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 18 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 @@ -33,6 +33,7 @@
import com.growingio.android.sdk.track.log.Logger;
import com.growingio.android.sdk.track.providers.ActivityStateProvider;
import com.growingio.android.sdk.track.providers.ConfigurationProvider;
import com.growingio.android.sdk.track.providers.EventBuilderProvider;
import com.growingio.android.sdk.track.providers.TrackerLifecycleProvider;
import com.growingio.android.sdk.track.utils.ActivityUtil;

Expand Down Expand Up @@ -60,6 +61,7 @@ private static class SingleInstance {
private static final PageProvider INSTANCE = new PageProvider();
}

private EventBuilderProvider eventBuilderProvider;
private ActivityStateProvider activityStateProvider;
private PageConfig pageConfig;

Expand All @@ -71,6 +73,7 @@ private PageProvider() {
@Override
public void setup(TrackerContext context) {
activityStateProvider = context.getActivityStateProvider();
eventBuilderProvider = context.getEventBuilderProvider();

ConfigurationProvider configurationProvider = context.getConfigurationProvider();
AutotrackConfig autotrackConfig = configurationProvider.getConfiguration(AutotrackConfig.class);
Expand Down Expand Up @@ -229,6 +232,12 @@ private void sendPage(Page<?> page) {
}

private void generatePageEvent(Page<?> page) {
Page<?> activePage = page.lastActivePage();
if (activePage != null) {
eventBuilderProvider.setCustomEventReferPage(activePage.path(), activePage.getShowTimestamp());
} else {
eventBuilderProvider.setCustomEventReferPage("/", 0);
}
String orientation = TrackMainThread.trackMain().getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT
? PageEvent.ORIENTATION_PORTRAIT : PageEvent.ORIENTATION_LANDSCAPE;
TrackMainThread.trackMain().postEventToTrackMain(
Expand Down
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 @@ -33,6 +33,7 @@ public class CoreConfiguration implements Configurable {
private int mSessionInterval = 30;
private boolean mDataCollectionEnabled = true;

private boolean mCustomEventWithPath = false;
private boolean mRequireAppProcessesEnabled = false;
private String mDataCollectionServerHost = "https://napi.growingio.com";
private EventFilterInterceptor mEventFilterInterceptor;
Expand Down Expand Up @@ -214,4 +215,16 @@ public CoreConfiguration setDataValidityPeriod(int dataValidityPeriod) {
this.mDataValidityPeriod = dataValidityPeriod;
return this;
}

/**
* Bring page path to custom event. If set true, the custom event will be associated with the page path.
*/
public CoreConfiguration setCustomEventWithPath(boolean enable) {
this.mCustomEventWithPath = enable;
return this;
}

public boolean isCustomEventWithPath() {
return mCustomEventWithPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Builder<T> setAttributes(Map<String, String> attributes) {
return this;
}

protected Map<String, String> getAttributes() {
public Map<String, String> getAttributes() {
return attributes;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ public class EventBuilderProvider implements TrackerLifecycleProvider {
private TrackerContext context;

private final AttributesBuilder generalProps = new AttributesBuilder();
private final CustomEventReferPage customEventReferPage = new CustomEventReferPage();

EventBuilderProvider() {
}

@Override
public void setup(TrackerContext context) {
configurationProvider = context.getConfigurationProvider();
customEventReferPage.isPageRefer = configurationProvider.core().isCustomEventWithPath();
this.context = context;
}

Expand Down Expand Up @@ -116,21 +118,47 @@ public void removeGeneralProps(String... keys) {
public BaseEvent onGenerateGEvent(BaseEvent.BaseBuilder<?> gEvent) {
dispatchEventWillBuild(gEvent);

if (!filterEvent(gEvent)) return null;
BaseEvent.BaseBuilder<?> eventBuilder = transformEventBuilder(gEvent);
if (!filterEvent(eventBuilder)) return null;

addDynamicPropsToAllEvent(gEvent);
addGeneralPropsToAllEvent(gEvent);
gEvent.readPropertyInTrackThread(context);
addDynamicPropsToAllEvent(eventBuilder);
addGeneralPropsToAllEvent(eventBuilder);

eventBuilder.readPropertyInTrackThread(context);
if (!configurationProvider.isDowngrade()) {
gEvent.readNewPropertyInTrackThread(context);
eventBuilder.readNewPropertyInTrackThread(context);
}

BaseEvent event = gEvent.build();
BaseEvent event = eventBuilder.build();
dispatchEventDidBuild(event);

return event;
}


public void setCustomEventReferPage(String pagePath, long timeStamp) {
customEventReferPage.pagePath = pagePath;
customEventReferPage.timeStamp = timeStamp;
}

private BaseEvent.BaseBuilder<?> transformEventBuilder(BaseEvent.BaseBuilder<?> gEvent) {
// only for custom event, exclude page level custom event
if (customEventReferPage.isPageRefer
&& gEvent instanceof CustomEvent.Builder
&& !(gEvent instanceof PageLevelCustomEvent.Builder)) {
CustomEvent.Builder customBuilder = (CustomEvent.Builder) gEvent;
PageLevelCustomEvent.Builder newBuilder = new PageLevelCustomEvent.Builder();
newBuilder.setAttributes(customBuilder.getAttributes());
newBuilder.setEventName(customBuilder.getEventName());
if (configurationProvider.isDowngrade()) {
newBuilder.setPageShowTimestamp(customEventReferPage.timeStamp);
}
newBuilder.setPath(customEventReferPage.pagePath);
return newBuilder;
}
return gEvent;
}

private void addDynamicPropsToAllEvent(BaseEvent.BaseBuilder<?> gEvent) {
if (dynamicGeneralPropsGenerator == null) return;
try {
Expand Down Expand Up @@ -276,4 +304,10 @@ String getEventPath(BaseEvent.BaseBuilder<?> eventBuilder) {
public void setDynamicGeneralPropGenerator(DynamicGeneralPropsGenerator dynamicGeneralPropsGenerator) {
this.dynamicGeneralPropsGenerator = dynamicGeneralPropsGenerator;
}

private static class CustomEventReferPage {
private boolean isPageRefer = false;
private String pagePath = "/";
private long timeStamp = 0L;
}
}

0 comments on commit 977ae04

Please sign in to comment.