Today, after reading APP Buried end visualization technology principle to go "fooling around" it.

Author | push a senior development engineer seventeen Andrews


One, background

Premise operators can analyze the user behavior, it is the mastery of large amounts of data. In the past, this data is usually in control by the developer clicks, and other events page, line by line Buried write code to complete the data collection. However, the traditional mode of operation whenever the upgrade and facelift, developers and testers need to be repeated constantly update the code, the entire process is time-consuming, can not meet the needs of the business.


To help developers address this pain point, a push Applied Statistics, "the number of" push "visual Buried" this technology to more efficiently implement this process. "The number of" visual Buried flexible, convenient, developers do not need to add any code to track data points, only need to connect the management station and circled page elements need to bury point, you can add track interface point force at any time.


This paper will combine the number of practical experience, two key technical points of the visual point that is buried controls uniquely identifies the event and collected for analysis and provide solutions.


Second, visualization key technical points Buried

Buried visual difficulties, or that is how the core in the case of developers do not write any code, SDK determine how any of the controls unique within the application, and how to monitor switch clicks and page controls.


Mark

To prevent the identification of different pages of repeated control, the control typically uniquely identified by a page identifier plus control identifier generation.

Page identifier generation

Page identifies the name of the page can be used directly, ie Activity name. Get more its way, presented here a more general way, that is by registering Application.ActivityLifecycleCallbacks, developers can callback the following lifecycle, and easily get the current Activity object. This method is applicable to a situation not Fragment Activity exist.


See the code below:

Obtaining is more, however, than in the Activity acquisition will be relatively trouble, because the system does not directly provide API, thus requiring slightly turn the corner: Gradle by way of instrumentation, access Fragment life cycle, and Fragment instance of an object:


If the page of the application in the presence of a nest multiple Fragment of Activity, Activity name alone may not be a precisely targeted to a page, which also need to add the name of the Fragment. Fragment acquisition instrumentation method may be implemented by Gradle, i.e. according to the object instance to obtain Fragment Fragment life cycle.



1.2 Controls logo generation

Ideally page for each control has its own unique id, SDK id direct access controls can identify as controls. But the reality is, there is often a page in the same id multiple controls, or no id controls, such as the Listview item, the developer can not give different settings for each item of listview id.


Therefore we need to change some ideas. We can start from the control path in addition to this id rather unique properties to build the control ID. Developers can add structure to uniquely identify the target angle control mode, the control generated by the path to the control. Below is a copy application station B on Github. We look for this application control tree analysis. First, we use the built-in Android Studio UI Automator Viewer tool to view the layout of the page:



Next, we can get a callback from Application.ActivityLifecycleCallbacks Activity instance, re-use activity.getWindow (). GetDecorView (). GetRootView () method to get the control tree of the current page.


FIG e.g. text is the TextView control, and no brother layout, can be labeled TextView [0]. Its parent row layout LinearLayout brothers and a second layout, it can be written as LinearLayout [. 1], and their splicing symbolic definition, such LinearLayout [1] / TextView [0]. After so on, loop through progressive layers, through all the controls and their subscript are spliced ​​together to form a unique identification controls in the page.


对于一些可复用的 View ,我们则需要采取一些特殊处理。例如对于 RecyclerView、ListView、 ViewPager 等复用控件,我们都需要采取不同的处理方式,去获取当前 View 在该控件中的具体下标。如果没有进行特殊处理,则会导致子控件错位,数据统计不准确。


采集

在以往的处理中,如果需要知道一个按钮的点击次数,开发者就要在该控件的click事件中加入对应的打点代码。这种重复劳作,无疑增加了开发者的开发负担。对此,我们可以采用动态代理方式或Gradle 插桩方式来改善这个问题。


动态代理方式


使用安卓自带的辅助功能 View.AccessibilityDelegate 。前文提到当页面变化时,我们可以通过 Application.ActivityLifecycleCallbacks 获取到 Activity 的实例对象,接着根据activity.getWindow().getDecorView().getRootView() 来获取到控件树。由于控件树可能会实时发生变化,我们则需要通过 ViewTreeObserver.OnGlobalLayoutListener 的方法监听视图变化,从而在该回调中拿到变化的控件。接着我们 要根据递归判断该控件是否为 ViewGroup、是否可以点击、是否能够显示等,继而给符合条件的 View 设置 sendAccessibilityEvent();此外,我们还要在继承了 View.AccessibilityDelegate 的定义类中,对以下这些方法添加 SDK 的代理:

当对应的控件被点击时,系统就会自动调用设置过代理的方法,存储或者上报对应数据。


Gradle 插桩的方式

Android Gradle 工具在1.5.0 版本后提供了 Transfrom API , 该API 允许第三方 Plugin 在打包 dex 文件之前的编译过程中操作 .class 文件。在编译期,开发者可以通过onClick、onItemClick等方法(详见下图)进行监听,这相当于是正则匹配。


当上述监听的方法被编译的时候,就可以将埋点的代理操作插入这些方法中,实现自动化埋点的流程。网上相关流程也是非常详细,有兴趣的可以自行搜索学习。


三、结语

以上就是APP端可视化埋点实现过程中的关键点,特别需要注意的是控件唯一标识那一块,由于布局千变万化,开发者针对很多特定的布局都需要采取对应的处理方式。


目前个推应用统计——个数这个产品只需要一行初始化代码就可以自动帮助开发者采集包括页面统计、事件埋点、新增活跃等多维度信息。


借着万圣节,悄悄跟你说个恐怖的故事:除个推应用统计服务之外,VIP消息推送、用户画像,现在申请均可以免费用一年!一键认证服务还可以享受充10万条送5万条的优惠哦 !点击https://www.getui.com/2019devfest,“码“上申请吧!



行业前沿、面试宝典,更多技术干货,尽在个推技术学院。



Guess you like

Origin blog.51cto.com/13031991/2446939