The background,

Operators are able to analyze user behavior on the premise of a large amount of data. In the past, this data was usually collected by developers writing line after line of buried code in the event of a control click, page, etc. However, when the traditional operation mode is updated, developers and testers need to update the code repeatedly, and the whole process is time-consuming and cannot meet the needs of the business.

To help developers address this pain point, The app Stats has launched a “visualization buried point” technology to make the process more efficient. The visual burying point of “number” is flexible and convenient. Developers do not need to add any code to the data tracking point. They only need to connect to the management console and circle the elements that need to be buried in the page to add the interface tracking point that takes effect at any time.

This paper analyzes and provides solutions to the two key technical points in the visualization buried point, namely, the unique identification of control and the event acquisition.

Two, visualization of buried point key technical points

The difficulty, or core, of visualization is how the SDK determines the uniqueness of any control within the application, and how it listens for clicks and page transitions, without the developer writing any code.

logo

In order to prevent the repetition of the control identity in different pages, the unique identity of the control is generally generated by the page identity plus the control identity.

Page identity generation

Page identifiers can use the name of the page directly, that is, the Activity name. The access is more, here introduce a more general way, namely Application through registration. ActivityLifecycleCallbacks, developers can in the following lifecycle callback, easily get the current object of the Activity. This method applies to an Activity that does not have a Fragment.

The code is shown below:

The system does not provide a direct API, so you need to take a little bit of a turn. Using Gradle, you can obtain the life cycle of your Fragment and its instance object:

If the page of the application has multiple fragments nested in an Activity, a single Activity name may not be able to accurately locate a page, so you need to add the Fragment name. Fragments can be obtained using Gradle piecing, which obtains Fragment instance objects based on the Fragment lifecycle.

1.2 Control identification generation

Ideally, each control on the page should have its own unique ID, and the SDK can directly obtain the control ID as the control identification. However, the reality is that a page often has multiple controls with the same ID, or controls with no ID, such as the Listview item. It is impossible for developers to assign a different ID to each Listview item.

So a shift in thinking is needed. We can start with the unique property of the control path other than the ID to generate the control identity. Developers can create unique identifiers for controls by adding control corner markers to their paths. The picture below is a Github application imitating B site. Let’s do a control tree analysis of this application. First, we use Android Studio’s BUILT-IN UI Automator Viewer to check the layout structure of the page:

Next, we can start the Application. The ActivityLifecycleCallbacks callback in the Activity instance, Then use the activity.getwindow ().getDecorView().getrootView () method to get the control tree for the current page.

For example, if the text control in the figure is a TextView and has no sibling layout, it can be marked as TextView[0]. LinearLayout[1] is the parent LinearLayout and is the second LinearLayout of its siblings, so you can write it as LinearLayout[1], and then use your own symbol stitching, like LinearLayout[1]/TextView[0]. After this analogy, cycle through, layer by layer, all through the control and their subscript are splicing together, constitute the control in the page unique identification.

For some reusable views, we need to take some special treatment. For example, RecyclerView, ListView, ViewPager and other reuse controls, we need to take different processing methods to obtain the current View in the control of the specific subscript. If there is no special processing, it will lead to child control dislocation, inaccurate data statistics.

collect

In the past, if you needed to know how many times a button was clicked, the developer would add the corresponding click code to the control’s click event. This kind of repeated work, undoubtedly increased the development burden of developers. To solve this problem, we can use dynamic proxy method or Gradle pile insertion method.

Dynamic proxy

Using android’s built-in auxiliary function View. AccessibilityDelegate. Mentioned above when the page changes, we can through the Application. The ActivityLifecycleCallbacks access to the Activity instance objects, The control tree is then retrieved from activity.getwindow ().getDecorView().getrootView (). Because of the real-time control tree may change, we need through the ViewTreeObserver. OnGlobalLayoutListener method to monitor the view changes, thus get a change of control in the callback. SendAccessibilityEvent (); sendAccessibilityEvent(); In addition, we also inherited the AccessibilityDelegate class, the definition of these methods to add the SDK agent:

When the corresponding control is clicked, the system will automatically call the method of setting the proxy, store or report the corresponding data.

Gradle piling

The Android Gradle tool provides the Transfrom API after version 1.5.0, which allows third-party plugins to manipulate.class files during compilation prior to packaging dex files. At compile time, developers can listen through onClick, onItemClick, etc. (see figure below), which is the equivalent of regular matching.

When the listening methods described above are compiled, the proxy operations for burying points can be inserted into these methods to automate the burying process. Online related process is also very detailed, interested in learning can search by themselves.

Three, endnotes

The above is the key point in the process of realizing the visual buried point of the APP end. Special attention should be paid to the unique identification of the control. Because of the ever-changing layout, developers need to take corresponding treatment methods for many specific layouts.

With just one line of initialization code, this product can automatically help developers collect multi-dimensional information including page statistics, event burying points, new activity and more.

With Halloween, quietly tell you a horror story: in addition to a tweet application statistics service, VIP message push, user portrait, now you can apply for free for one year! One-key authentication service can also enjoy the charge of 100,000 to send 50,000 preferential oh! Click https://www.getui.com/2019devfest, “code” on the application!

Industry forefront, interview treasure book, more technical dry goods, all in a push technical college.