Buried point

In this era of big data, data is the core of the development of the Internet. In addition to the importance of customer analysis, it is also very important for a company to objectively control its own products.

Embedding means injecting code at key points in the project that sends device information, user actions, time points, and so on to the server.

Code buried point

The easiest way to do this is to have the developer add a line of code where it is needed to make a request to the server

Automatic submerged point

Buried code is not directly related to business logic code, and having developers manually add code to a project increases project coupling. Not only developers trouble, late maintenance also increased the difficulty. Therefore, it is more reasonable to automatically inject buried points at the most common locations by introducing external code.

In terms of web side, generally selected injection points include: page loading completion, users click the link, login and logout and other scenarios.

Applets life cycle

Applets App functions: onLaunch, onShow, onHide, onError, onPageNotFound five cycle methods, the applets will use the onLaunch method.

The small program has a background mechanism. When the user closes the current small program and returns to the wechat page, the small program will not directly end the process, but wechat will automatically destroy the small program when the memory usage reaches a certain amount.

OnShow and onHide correspond to the conversion between front and back of the applet. When the user transfers from wechat to the applet, onShow will be called. On the contrary, onHide will be called when the user goes from the mini program to the wechat interface.

Small program each Page is a Page, each Page are: onLoad onReady/onShow onHide/onUnload/onShareAppMessage/onPullDownRefresh etc cycle method.

Small program buried point

In the small program each life cycle buried point, can effectively collect user operation data, normal small program developers, these cycle methods are declared by the developer. Developers can write the reporting server code in the cycle method, but as mentioned earlier, it is better to do this from external code. So we need some functional code to achieve automatic injection of buried points.

App and Page functions

The applet is like a custom webView. When the App is opened, the applet first calls the declared App function, so we can inject the buried point by hijacking the App function.

var oldApp = App
App = function(args) { ... OldApp (args)}Copy the code

As shown in the above code, after hijacking the App function, rewrite the periodic function, and finally run the real App function. Args and the model configured for the applet app.js contains the app’s periodic functions and globalData objects, for example, to inject buried points into the onLaunch method:

functionCustomLaunch (opt) {track() // Report to the server} var oldLaunch = args. OnLaunch argsfunction(opt) {
    if(oldLaunch) {oldlaunch.call (this, opt)} customlaunch.call (this, opt) // Custom cycle method}Copy the code

Other periodic methods can also be rewritten in this way.

Extend automatic burying point

The requirements of each application are different, so the ability to customize automatic burying points is also needed. In each page of index.js, we can use getApp() to get the applet global object. This in App refers to the global object, so we can define a custom buried method in the code block above:

--- app.js
args.onLaunch = function(opt) {
    this[track] = function() {... // Custom embedded code}... } -- Page index.js var app = getApp() app.track(name) // Send custom packetsCopy the code

Compared with web applications, the natural life cycle of small programs is much more friendly to burying points. How to control and realize visual burying points is the direction we need to work hard.