search
HomeWeChat AppletMini Program DevelopmentMini Program Practice: Implementing a Simple Mini Program Monitoring Function

This article brings you a practical application of a small program and teaches you how to implement a simple small program monitoring function. I hope it will be helpful to you!

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Before this, once our mini program reported an error, it all relied on user screenshot feedback, and the developer would reproduce it bit by bit

and think about it later. , is there something that can record the user's behavior? If the mini program reports an error, it will automatically upload the user's operation record to the server and notify the developer to handle it?

I later learned that this is called front-end monitoring.

Of course, this article talks about relatively simple ones, because I don’t know how to do it and I haven’t participated in the development of a real project yet. .

What are the functions of this small program monitoring?

1) Record the time when the user enters and exits the page
2) Monitors all click events
3) Record the approximate operation trajectory of the user.
4) Cloud function call failures are automatically reported to the database to remind developers to handle them.

Realize the recording of users entering and exiting the page

We all know that the mini program has several life cycle functions. Among them, I chose the three life cycles of onShow, onHide and unload. Record periodically.

The stupider way is to directly record the time when the page is displayed and hidden/unloaded in the life cycle function of each page, but this is too repetitive,

So we can give these few Then add another layer to each life cycle function (this is called the decorator pattern in Javascript design pattern)

A new question comes again, how to add another layer to all events?

Let’s first look at the index.js file of a page

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

By passing an object to the Page method, This object contains all events (click, slide, CSS3 animation, etc.) and life cycle.

So we can customize a method to replace the Page method. In this method, get the object passed in and modify it. Finally, remember to execute the original Page(Obj) again. Look at the code structure

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

#The code is actually very simple. Once the function is called, the cache is read. If the data exists, the information of the current page is appended to the array element. If the array length is greater than 10, remove the first element and keep the array length at 11.

The reason why a timer is used is because sometimes the latest data cannot be read if the timer is not added during the test. Get the old data, modify it, assign it, and finally reset the cache (because when the onshow function is executed, the onhide function of the previous page may not be completed, and this function will modify the cache, so the onshow function will get is not the latest cache, resulting in information loss.)

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Look at the cache results:

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Implement monitoring of all click events

1) The simplest way is to use the publish and subscribe model, but it is too troublesome.

2) Add another layer to all events in the page. When the event is triggered, there will be a parameter e, just judge e.type.

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Look at the code

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

##Finally returns the call to the original function, Next, take a look at the replaceOld function. What it implements is to wrap the original event (decorator mode)

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Execute replace for each function pair in the page this way.

what's the function?

We can see that this function wraps the original method. The specific method of packaging depends on the replacement function passed in.

Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

This The function finally returns the execution of the original function, so the content of the package is the judgment of whether it is a click event in the function body. If yes, just save the data.

1Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

See cached results:

1Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

Cloud function call failures are automatically reported to the database to remind developers to handle them.

Use Object.defineProperty() to hijack the cloud function call, wrap one more layer and then return the cloud function call

But there is a point to note here, the cloud function call There are two ways,

1) There is a callback function passed in, and the result is obtained in the callback function.

2) If there is no callback function passed in, await is used to wait for the call result, and we need to capture the error of the cloud function call,

So we get the result directly during the hijacking and then return one Promise.

1Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

#Automatically notify developers and process it. It is actually very simple to call the template message provided by WeChat in the cloud function.

Look at the cached results

1Mini Program Practice: Implementing a Simple Mini Program Monitoring Function

The structure may be a bit messy, after all, it is the first time I write it and it has not been applied in practice yet.

[Related learning recommendations: 小program development tutorial

The above is the detailed content of Mini Program Practice: Implementing a Simple Mini Program Monitoring Function. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.