Remember to solve a small program live plug-in page routing problem
background
Routing buried point scheme
OnAppRoute for route switching track, wx.onAppShow and wx.onAppHide for opening/closing/returning pages for the first time.
When used in business, it is found that there is a gap of several times between the data of the plug-in page and the official data of wechat:
platform | PV | UV |
---|---|---|
774 | 115 | |
Their own | 214 | 45 |
To solve
Experimental problem finding
According to the experiment, it is concluded that there are problems in plug-in page burying point:
- Directly open the live broadcast plug-in page
- Failed to report routing events. Procedure
- Subsequent routing events cannot be reported
- Other pages Enter the live broadcast plug-in page (normal)
- Open other non-plug-in pages directly (normal)
Doubt/explanation (because knowledge of the existence of a problem is gradual, not all problems are known at once, so are doubts raised)
- Due to the live broadcast and network conditions, some data cannot be reported (some data based on the live broadcast page should be blamed 🐶)
- Open the page directly this situation has its own buried point cannot support
- Plug-in pages are different from normal pages
Propose solutions:
-
Solve the problems existing in its own buried points (unknown cost upper solution)
-
Use third party burying points: Friendly Alliance, Aladdin… See if they can support plug-in page routing event reporting (low cost buried data in third party)
-
Live broadcast plug-in provides components, according to these components to achieve their own live page (high development cost)
1 and 2 can be done at the same time, 3 is really an option
Access to the third party buried point, after the test to see the data, direct good guy, do not support plug-in page routing events report
Solve the problems existing in their own burial points:
-
The routing event fails to be reported when the live broadcast plug-in page is directly opened
Wrap App function
// App const originalApp = App App = function(this: any, obj: Object) { xProxy(obj, 'onLaunch', captureOnLoad) return originalApp.apply(this.arguments)}Copy the code
-
Route events after the live broadcast plug-in page is opened cannot be reported
-
Real machine test to find the cause -> see if there is an error message
-
Read the documentation and guess what is the limit of the plug-in’s API call
-
Test the plugin page registered wx. OnAppRoute/wx. OnAppRouteDone wx. OnAppShow and wx. Although onAppHide didn’t complain, but subsequent won’t trigger
Solution: Wrap the Page (without affecting the plug-in Page) and re-initialize the route report when the startup Page is the plug-in Page
let needReInit = false function captureOnLoad(args: any) { if (args.path && isPluginPage(args.path)) { needReInit = true // The route needs to be reported by itself}}function xProxy(target: any, method: string, customMethod: Function) { const originMethod = target[method] // eslint-disable-next-line no-constant-condition if (originMethod || 1) { target[method] = function() { originMethod && originMethod.apply(this.arguments) customMethod.apply(this.arguments)}}}function onPageLoad(this: any) { if (needReInit) { // reinitialize the event listener X.retrack() needReInit = false // The route needs to be reported by itself}}// App const originalApp = App App = function(this: any, obj: Object) { xProxy(obj, 'onLaunch', captureOnLoad) return originalApp.apply(this.arguments)}// Page const originalPage = Page Page = function(this: any, obj: Object) { xProxy(obj, 'onLoad', onPageLoad) return originalPage.apply(this.arguments)}Copy the code
-
The test release
npm run test && npm run deploy
error ...
Copy the code
-
Stability of live broadcast and stable regression of other core processes
-
Directly open the live plug-in page to report routing events
-
The route event reporting function is displayed on the live broadcast plug-in page
rollover
The subsequent routing events were not reported. Obviously, the demo in the buried site project was fine, but the car was turned over in the business project.
Compare the differences between the demo and the business project, only the Taro version of the problem, demo is Taro 3, the business project is Taro 2.
Check Taro 2.x’s code for pages/components and discover that all Taro 2.x components and pages are wrapped in Component
component.js#L46
create-component.js#L396
I’m fucking straight up, man!
Ok, wrap Component again:
function onComponentPageShow(this: any) {
// Determine if it is a page
if (needReInit && this.$component.$componentType === 'PAGE') {
// reinitialize the page burying point
X.retrack()
needReInit = false
// The route needs to be reported by itself}}// Component
const originalComponent = Component
Component = function(this: any, obj: any) {
obj.pageLifetimes = obj.pageLifetimes || {}
xProxy(obj.pageLifetimes, 'show', onComponentPageShow)
return originalComponent.apply(this.arguments)}Copy the code
This time it will work!
Verification of real data:
platform | PV | UV |
---|---|---|
579 | 112 | |
Their own | 431 | 113 |
PV gap causes and correction ideas:
Cause: The buried routing point is not reported when the live broadcast page is opened repeatedly (after opening, cut the background and come back)
Wx. onAppShow and wx.onAppHide switch to App onShow and onHide
Conclusion:
- The plug-in page is different from the normal page:
- Limitations of plug-in call API launch page is plug-in page need to consider re-registration
- Plug-in page
Page
With the globalPage
Is isolated, third party based packagingPage
It is impossible to get the routing burials of plug-ins (must be usedwx.onAppRoute
)
- The Taro 2 page is used
Component
Implementation of the