Skip to content

Commit

Permalink
feat: page rule (#211)
Browse files Browse the repository at this point in the history
* make downgrade as cdp config.

* cdp for 4.0

* upload index when > -1

* optInt or optLong has default value from annotation.

* add page rule.
1. load page setting from xml file;
2. add config method for page setting;
3. call track app set page.

* attributes.remove(null) => null check


---------

Co-authored-by: tianhui12 <[email protected]>
  • Loading branch information
cpacm and tianhui12 authored Apr 2, 2024
1 parent 3795f39 commit c09891d
Show file tree
Hide file tree
Showing 15 changed files with 496 additions and 36 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ subprojects {
spotless {
java {
target '**/*.java'
licenseHeaderFile rootProject.file('gradle/java.header')

removeUnusedImports()
}
Expand All @@ -63,7 +62,6 @@ subprojects {
targetExclude("$buildDir/**/*.kt")

ktlint()
licenseHeaderFile rootProject.file('gradle/java.header')
}
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 @@ -29,7 +29,7 @@ powermock = "2.0.9"
# !!! SDK VERSION !!!
growingio = "4.2.0"
growingioCode = "40200"
growingioPlugin = "4.0.0"
growingioPlugin = "4.1.0"

[plugins]
android-application = { id = "com.android.application", version.ref = "pluginGradle" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,25 @@ internal class JsonSerializerGenerator(
),
)
toJsonMethod.beginControlFlow("if($fieldName != null && !$fieldName.isEmpty())")
.addStatement("$fieldName.remove(null)")
.addStatement(
"jsonObject.put(\"${fieldName}\", new \$T(event.$fieldMethod))",
"\$T keys = $fieldName.keySet()",
ParameterizedTypeName.get(
ClassName.get(Set::class.java),
ClassName.get(String::class.java),
),
)
.addStatement(
"\$T attrObject = new \$T()",
ClassName.get(JSON_OBJECT_PACKAGE, JSON_OBJECT_CLASS),
ClassName.get(JSON_OBJECT_PACKAGE, JSON_OBJECT_CLASS),
).endControlFlow()
)
.beginControlFlow("for (String key : keys)")
.beginControlFlow("if (key != null)")
.addStatement("attrObject.put(key, $fieldName.get(key))")
.endControlFlow()
.endControlFlow()
.addStatement("jsonObject.put(\"${fieldName}\", attrObject)")
.endControlFlow()
} else {
toJsonMethod.addStatement(
"jsonObject.put(\"${fieldName}\", new \$T(event.$fieldMethod))",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@
package com.growingio.android.sdk.autotrack;

import com.growingio.android.sdk.Configurable;
import com.growingio.android.sdk.autotrack.page.PageRule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AutotrackConfig implements Configurable {
private float mImpressionScale = 0;
private boolean enableFragmentTag = false;
private final AutotrackOptions mAutotrackOptions = new AutotrackOptions();

private int pageXmlRes = 0;
private final List<PageRule> pageRules = new ArrayList<>();

public AutotrackConfig setImpressionScale(float scale) {
if (scale < 0) {
scale = 0;
Expand All @@ -36,6 +44,76 @@ public float getImpressionScale() {
return mImpressionScale;
}

/**
* Set the page rule xml resource id for auto page track.
*
* @param pageXmlRes page rule xml resource id
*/
public AutotrackConfig setPageRuleXml(int pageXmlRes) {
this.pageXmlRes = pageXmlRes;
return this;
}

/**
* Add page rule for auto track.
* PageName is the alias name of the page, such as "MainPage".
* PageClassPath is the full path of the page class, such as "com.growingio.android.sdk.autotrack.MainActivity".
*
* @param pageName page's alias name
* @param pageClassPath page's class path
* @return AutotrackConfig
*/
public AutotrackConfig addPageRule(String pageName, String pageClassPath) {
pageRules.add(new PageRule(pageName, pageClassPath));
return this;
}

/**
* Add page rule for auto track.
* pageClassRegex support regular expression, such as "com.growingio.android.autotrack.*Activity".
*
* @param pageClassRegex page's regular expression
*/
public AutotrackConfig addPageMatchRule(String pageClassRegex) {
pageRules.add(new PageRule(pageClassRegex));
return this;
}

/**
* Add page rule for auto track.
* PageName is the alias name of the page, such as "MainPage".
* PageClassPath is the full path of the page class, such as "com.growingio.android.sdk.autotrack.MainActivity".
* PageAttributes is the attributes of the page, such as "{'key1':'value1','key2':'value2'}".
*
* @param pageName page's alias name
* @param pageClassPath page's class path
* @param attributes page's attributes
* @return AutotrackConfig
*/
public AutotrackConfig addPageRuleWithAttributes(String pageName, String pageClassPath, Map<String, String> attributes) {
PageRule pageRule = new PageRule(pageName, pageClassPath);
pageRule.setAttributes(attributes);
pageRules.add(pageRule);
return this;
}

/**
* Add page rule for auto track.
* pageClassRegex support regular expression, such as "com.growingio.android.autotrack.*Activity".
* PageAttributes is the attributes of the page, such as "{'key1':'value1','key2':'value2'}".
*
* @param pageClassRegex page's regular expression
* @param attributes page's attributes
* @return AutotrackConfig
*/
public AutotrackConfig addPageMatchRuleWithAttributes(String pageClassRegex, Map<String, String> attributes) {
PageRule pageRule = new PageRule(pageClassRegex);
pageRule.setAttributes(attributes);
pageRules.add(pageRule);
return this;
}


public AutotrackConfig setWebViewBridgeEnabled(boolean webViewBridgeEnabled) {
mAutotrackOptions.setWebViewBridgeEnabled(webViewBridgeEnabled);
return this;
Expand All @@ -59,4 +137,11 @@ public boolean isEnableFragmentTag() {
return enableFragmentTag;
}

public List<PageRule> getPageRules() {
return pageRules;
}

public int getPageXmlRes() {
return pageXmlRes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@
import android.text.TextUtils;
import android.view.View;

import com.growingio.android.sdk.autotrack.AutotrackConfig;

public class ActivityPage extends Page<SuperActivity> {

private AutotrackConfig autotrackConfig;
private PageConfig pageConfig;

public ActivityPage(Activity carrier) {
super(new SuperActivity(carrier));
}

public ActivityPage(Activity carrier, AutotrackConfig autotrackConfig) {
public ActivityPage(Activity carrier, PageConfig pageConfig) {
super(new SuperActivity(carrier));
this.autotrackConfig = autotrackConfig;
this.pageConfig = pageConfig;

if (this.pageConfig != null) {
String fullPageClassPath = getCarrier().getRealActivity().getClass().getName();
loadPageRule(this.pageConfig.getPageRules(), fullPageClassPath);
}
}

@Override
Expand All @@ -51,7 +55,7 @@ public String getClassName() {
@Override
public boolean isAutotrack() {
// cdp downgrade when activity page is enabled
if (autotrackConfig != null && autotrackConfig.getAutotrackOptions().isActivityPageEnabled()) {
if (pageConfig != null && pageConfig.isActivityPageEnabled()) {
return !isIgnored();
}
return super.isAutotrack();
Expand All @@ -78,4 +82,13 @@ public String getTitle() {
}
return super.getTitle();
}

@Override
public String pagePath() {
if (pageConfig != null && pageConfig.isDowngrade()) {
return originPath(true);
} else {
return "/" + getName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@
import android.text.TextUtils;
import android.view.View;

import com.growingio.android.sdk.autotrack.AutotrackConfig;

public class FragmentPage extends Page<SuperFragment<?>> {

private AutotrackConfig autotrackConfig;
private PageConfig pageConfig;

public FragmentPage(SuperFragment<?> carrier) {
super(carrier);
}

public FragmentPage(SuperFragment<?> carrier, AutotrackConfig autotrackConfig) {
public FragmentPage(SuperFragment<?> carrier, PageConfig pageConfig) {
super(carrier);
this.autotrackConfig = autotrackConfig;
this.pageConfig = pageConfig;
if (this.pageConfig != null) {
String fullPageClassPath = getCarrier().getRealFragment().getClass().getName();
loadPageRule(this.pageConfig.getPageRules(), fullPageClassPath);
}
}

@Override
Expand All @@ -53,7 +55,7 @@ public View getView() {

@Override
public String getTag() {
if (autotrackConfig.isEnableFragmentTag()) {
if (pageConfig.isEnableFragmentTag()) {
String tag = getCarrier().getTag();
if (!TextUtils.isEmpty(tag)) {
return transformSwitcherTag(tag);
Expand Down Expand Up @@ -87,9 +89,18 @@ private String transformSwitcherTag(String tag) {
@Override
public boolean isAutotrack() {
// cdp downgrade when fragment page is enabled
if (autotrackConfig != null && autotrackConfig.getAutotrackOptions().isFragmentPageEnabled()) {
if (pageConfig != null && pageConfig.isFragmentPageEnabled()) {
return !isIgnored();
}
return super.isAutotrack();
}

@Override
public String pagePath() {
if (pageConfig != null && pageConfig.isDowngrade()) {
return originPath(true);
} else {
return "/" + getName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.PatternSyntaxException;

public abstract class Page<T> {
private final static int MAX_PAGE_LEVEL = 3;
Expand Down Expand Up @@ -256,7 +257,41 @@ public String path() {
return mPath;
}

this.mPath = originPath(true);
this.mPath = pagePath();
return this.mPath;
}

protected String pagePath() {
return "/" + getClassName();
}

protected void loadPageRule(List<PageRule> pageRuleList, String fullPageClassPath) {
if (pageRuleList == null || fullPageClassPath == null) return;
// match exactly page classpath at first
for (PageRule pageRule : pageRuleList) {
if (!pageRule.isRegMatch() && fullPageClassPath.equals(pageRule.getPageClassPath())) {
setAlias(pageRule.getPageName());
setAttributes(pageRule.getAttributes());
setIsAutotrack(true);
}
}
if (!mIsAutotrack) {
// match reg page classpath secondly
for (PageRule pageRule : pageRuleList) {
if (pageRule.isRegMatch() && matchPageRule(fullPageClassPath, pageRule.getPageClassPath())) {
setAlias(getName());
setAttributes(pageRule.getAttributes());
setIsAutotrack(true);
}
}
}
}

private boolean matchPageRule(String input, String regex) {
try {
return input.matches(regex);
} catch (PatternSyntaxException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024 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.autotrack.page;

import java.util.List;

class PageConfig {
private boolean enableFragmentTag = false;
private List<PageRule> pageRuleList;
private boolean isActivityPageEnabled = true;
private boolean isFragmentPageEnabled = true;
private boolean isDowngrade = false;

public PageConfig(List<PageRule> pageRuleList, boolean isActivityPageEnabled, boolean isFragmentPageEnabled, boolean enableFragmentTag, boolean isDowngrade) {
this.pageRuleList = pageRuleList;
this.isActivityPageEnabled = isActivityPageEnabled;
this.isFragmentPageEnabled = isFragmentPageEnabled;
this.enableFragmentTag = enableFragmentTag;
this.isDowngrade = isDowngrade;
}

public boolean isEnableFragmentTag() {
return enableFragmentTag;
}

public List<PageRule> getPageRules() {
return pageRuleList;
}

public boolean isActivityPageEnabled() {
return isActivityPageEnabled;
}

public boolean isFragmentPageEnabled() {
return isFragmentPageEnabled;
}

public boolean isDowngrade() {
return isDowngrade;
}
}
Loading

0 comments on commit c09891d

Please sign in to comment.