当前位置: 首页 > article >正文

监控平台之pvuv/点击事件/路由上报

pv/uv上报方式
// 这里是pv的上报方式
// 可以用自定义标签定义data-spider,通过body-parser等方式获取body标签的值,进行上报
// 

export default function pv() {
    const reportData = {
        type: 'behavior',
        subType: 'pv',
        startTime: performance.now(),
        pageUrl: window.location.href,
        uid: generateUniqueId(),
        referrer: window.referrer,
    }
    lazyReportBatch(reportData) 
}

// 可以自定义按钮的上报函数,暴露给外部调用
export default function funPV(options) {
    const reportData = {
        type: 'behavior',
        subType: 'pv',
        startTime: performance.now(),
        pageUrl: window.location.href,
        uid: generateUniqueId(),
        referrer: window.referrer,
    }
    for (const key in options) {
        if (reportData.hasOwnProperty(key)) {
            reportData[key] = options[key];
        }
    }
    lazyReportBatch(reportData) 
}
点击事件上报

监听mousedown,touchstart进行监听点击事件,

import {lazyReportBatch} from "../report"

export default function click() {
    ['mousedown', 'touchstart'].forEach(eventType => {
        window.addEventListener(eventType, e => {
            const target = e.target;
            if (!target.tagName) {
                return
            }
            const reportData = {
                type: 'behaviour',
                subType: 'click',
                pageUrl: window.location.href,
                target: target.tagName,
                clientX: e.clientX,
                clientY: e.clientY,
                startTime: e.timeStamp,
                innerHtml: target.innerHtml,
                outerHtml: target.outerHtml,
                width: target.offsetWidth,
                height: target.offsetHeight,
                path: e.path
            }
            lazyReportBatch(reportData) 
        })
    })
}
路由上报

分为hashchange和popstate,根据是否有#进行分别,区别如下

hashChange 触发条件

hash是URL中#后面那部分,同时修改hash值不会引起页面的刷新,也不会向服务器重新发送请求。通过hashchange事件可以监听它的变化。改变hash值有以下三种方式:

  • 浏览器前进后退改变URL
  • 通过a标签锚点方式改变URL。
  • 通过window.location.hash改变URL
  • 调用history的back、go、forward方法

不能触发事件的方式

  • pushState和replaceState

备注:以上三种方式均可以触发hashchang事件, pushState和replaceState均不能触发hashchang事件

popstate触发条件

history提供了popstate监听事件,但是只有以下两种情况会触发该事件

  • 点击浏览器前进后退的按钮
  • 显示调用history的back、go、forward方法
  • 不能触发事件的方式

pushState和replaceState

  • a标签不能触发,因为非锚点模式直接跳转了页面。

这里记录了路由的from路径和to路径,根据业务具体需求上报参数

import {lazyReportBatch} from "../report"
import {generateUniqueId} from "../../utils"

export default function pageChange(data) {
    // hash history

    let oldUrl = ''
    window.addEventListener('hashchange', function(event) {
        const newUrl = window.location.href;
        const reportData = {
            from: oldUrl,
            to: newUrl,
            type: 'behavior',
            subType: 'hashchange',
            pageUrl: window.location.href,
            startTime: this.performance.now(),
            uuid: generateUniqueId(),
        }
        lazyReportBatch(reportData) 
        oldUrl = newUrl;
    }, true); 

    // 点击后退前进的时间
    let from = ''
    window.addEventListener('poststate', function(event) {
        const to = window.location.href;
        const reportData = {
            from,
            to,
            type: 'behavior',
            subType: 'poststate',
            pageUrl: window.location.href,
            startTime: this.performance.now(),
            uuid: generateUniqueId(),
        }
        lazyReportBatch(reportData) 
        from = to;
    }, true); 
} 


http://www.kler.cn/news/289605.html

相关文章:

  • 树莓派扩展RGB点阵屏的使用
  • vue-----window.open打开新窗口文件并且修改窗口标题下载文件
  • 云微客短视频矩阵系统,如何让企业赢在起跑线?
  • Flask框架默认session处理机制
  • gRPC学习之六:gRPC-Gateway集成swagger
  • 山东省行政执法证照片要求及图像处理方法
  • 实训day40(8.30)
  • 数据结构-广义表
  • 在uni-app中使用SQLite
  • office套件打开时 提示操作系统当前的配置不能运行此应用程序
  • VS2022使用指定的LLVM版本
  • windows 环境下安装OpenCV For Java
  • 一些可能很有用的矩阵知识
  • 新手c语言讲解及题目分享(十)——数组专项练习
  • 免费GPU平台教程,助力你的AI, pytorch tensorflow 支持cuda
  • Vue3组件通信概览
  • [Raspberry Pi]如何利用docker執行motioneye,並利用Line Notify取得即時通知和照片?
  • 2024车牌识别系统十大品牌发布,车牌智能识别系统品牌哪家好?
  • 【408 数据结构】第1章绪论
  • 开发基础软件安装地址(持续更新中)
  • Spring Boot实战:运用享元模式优化微服务间共享资源
  • 使用VM创建centos7环境
  • 知识点复习3
  • 速盾:什么是高防cdn?高防cdn的特点和好处有哪些?
  • 微信小程序知识点(一)
  • 自然语言处理系列五十三》文本聚类算法》文本聚类介绍及相关算法
  • 【软件文档】系统安全保证措施(Word)
  • 【代码随想录训练营第42期 Day49打卡 - 接雨水和柱状图最大矩形问题
  • HTML基本概述
  • 2024升级zblog小程序开源源码/基于uniapp开发的(支持微信小程序、百度小程序、安卓APP)