vue 数据埋点

2024-04-04 08:12

本文主要是介绍vue 数据埋点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近菜鸟做项目,需要做简单的数据埋点,不是企业级的,反正看渡一的视频,企业级特别复杂,包括但不限于:错误收集、点击地方、用户行为……

菜鸟的需求就是简单收集一下用户的ip、地址、每个界面的访问时间,而且面向的是全球用户,所以自己简单写了。

文章目录

  • 一、获取IP、地址
  • 二、数据埋点
    • 解决传入的是跳转后的界面
    • 解决监听不到关闭网页的问题
  • 三、按钮埋点

一、获取IP、地址

这里菜鸟最开始想到的就是高德、腾讯地图这样的平台,里面都是有ip逆向解析的,但是菜鸟一看高德的api,发现不支持国外!!!
在这里插入图片描述
然后百度虽然支持,但是也不是很准确,最重要的一点是总是提示我跨域了,不知道咋解决!!!
在这里插入图片描述
所以菜鸟就在网上找到一个免费且比较准确的

https://ipinfo.io/account/profile

反正电脑不是手机,定位确实不好搞,反正菜鸟感觉定位的地方就是离你最近的转发基站的位置,而不是你本来的位置!

希望有知道可以精确定位的读者可以 指点江山,激扬文字

二、数据埋点

这里菜鸟最开始想的是 vue3 的 mixins,发现 vue3 不推荐 mixins,但是可以直接使用组合式 API 的组合式函数,就相当于把mixins当函数提出去,然后需要用到的时候直接引入就行!

然后菜鸟写的代码就是这样的了:
src/mixins/pagetime.js

/*** 书写界面停留时间函数*/
import { onBeforeUnmount } from "vue";
import { DataburialpointApi } from "@/network/api";
import { useRoute } from "vue-router";// 按照惯例,组合式函数名以“use”开头
export function usePageTime() {const _routeObj = {"/content": "首页","/services/0": "NGS- Whole Genome Sequencing","/services/1": "NGS- mRNA Sequencing","/services/2": "NGS- Small RNA Sequencing","/services/3": "NGS-Lnc RNA Sequencing","/services/4": "NGS- Metagenome Sequencing","/services/5": "NGS- Microbial diversity Sequencing","/services/6": "NGS- Hi-C Sequencing","/services/7": "Nanopore- Nanopore Ultra-long Sequencing","/services/8": "Nanopore- Nanopore Sequencing","/services/9": "Nanopore- Direct RNA Sequencing","/services/10": "Nanopore- Lnc RNA Sequencing","/services/11": "Nanopore- CircRNA Sequencing","/services/12": "Nanopore- TAIL Iso Sequencing","/services/13": "Nanopore- Isoform Sequencing","/services/14": "Nanopore- Direct-CDNA Sequencing","/services/15": "Nanopore- Single-cell full-length transcriptome Sequencing","/services/16": "Nanopore- Full-length 16S/18S/ITS Amplicon Sequencing","/services/17": "Nanopore- 16S-23S Amplicon Metagenomic Sequencing","/services/18": "Nanopore- Metagenome Sequencing","/services/19": "Nanopore- PORE-C Sequencing","/services/20": "Pacbio Revio- Revio Sequencing","/resources/sample": "资源-样本要求","/about": "关于我们","/contactus": "联系我们",};let _startTime = new Date();let _useTime = 0;// 路由菜单相关let route = useRoute();onBeforeUnmount(() => {_useTime = new Date() - _startTime;console.log(_routeObj[route.path]);console.log(_useTime);let _formdata = {};_formdata.visitorId = localStorage.getItem("userId");_formdata.url = route.path;_formdata.classify = _routeObj[route.path];_formdata.time = _useTime;DataburialpointApi(_formdata).then((res) => {console.log(res);}).catch((err) => {console.log(err);});});
}

结果压根不靠谱,这样发送给后端的是跳转后的界面,而不是跳转前的,所以每次都是把上一个界面的访问时间和新跳转的界面发送过去了!!!而且还有一个bug 就是别人不切换界面,那我就不知道别人上一个界面访问了多久,且当别人访问一个界面后,直接点击关闭标签,也无法知道这个界面访问了多久

接下来一个一个解决!

解决传入的是跳转后的界面

菜鸟发现app.vue里有监听路由,那岂不是可以监听路由改变的时候发一个emitter,然后这里监听到了,取旧的值不就行了?于是代码变成了这样:
src/mixins/pagetime.js

/*** 书写界面停留时间函数*/
import { ref, onBeforeUnmount } from "vue";
import { DataburialpointApi } from "@/network/api";
import emitter from "@/tools/eventBus";// 按照惯例,组合式函数名以“use”开头
export function usePageTime() {const _routeObj = {"/content": "首页","/services/0": "NGS- Whole Genome Sequencing","/services/1": "NGS- mRNA Sequencing","/services/2": "NGS- Small RNA Sequencing","/services/3": "NGS-Lnc RNA Sequencing","/services/4": "NGS- Metagenome Sequencing","/services/5": "NGS- Microbial diversity Sequencing","/services/6": "NGS- Hi-C Sequencing","/services/7": "Nanopore- Nanopore Ultra-long Sequencing","/services/8": "Nanopore- Nanopore Sequencing","/services/9": "Nanopore- Direct RNA Sequencing","/services/10": "Nanopore- Lnc RNA Sequencing","/services/11": "Nanopore- CircRNA Sequencing","/services/12": "Nanopore- TAIL Iso Sequencing","/services/13": "Nanopore- Isoform Sequencing","/services/14": "Nanopore- Direct-CDNA Sequencing","/services/15": "Nanopore- Single-cell full-length transcriptome Sequencing","/services/16": "Nanopore- Full-length 16S/18S/ITS Amplicon Sequencing","/services/17": "Nanopore- 16S-23S Amplicon Metagenomic Sequencing","/services/18": "Nanopore- Metagenome Sequencing","/services/19": "Nanopore- PORE-C Sequencing","/services/20": "Pacbio Revio- Revio Sequencing","/resources/sample": "资源-样本要求","/about": "关于我们","/contactus": "联系我们",};let _startTime = new Date();let _useTime = ref(0);emitter.on("pageTime", (oldValue) => {_useTime.value = new Date() - _startTime;console.log(_routeObj[oldValue]);console.log(_useTime.value);let _formdata = {};_formdata.visitorId = localStorage.getItem("userId");_formdata.url = oldValue;_formdata.classify = _routeObj[oldValue];_formdata.time = _useTime.value;DataburialpointApi(_formdata).then((res) => {console.log(res);}).catch((err) => {console.log(err);});});onBeforeUnmount(() => {emitter.off("pageTime");});
}

但是菜鸟一想,不对呀,全部和路由相关,直接写路由里面不好吗?这样写成mixins还要每个界面都这这样引入!

// mixin
import { usePageTime } from "@/mixin/pagetime";
usePageTime();

所以直接用路由守卫了,代码如下:
src/mixins/pagetime.js

import { createRouter, createWebHashHistory } from "vue-router";
import Home from "@/views/home/home.vue";
import { DataburialpointApi } from "@/network/api";const routes = [{path: "/",name: "home",redirect: "/content",component: Home,children: [{path: "/content",name: "content",component: () => import("../views/content/content.vue"),},{path: "/services/:type",name: "services",component: () => import("../views/services/services.vue"),},{path: "/resources",name: "resources",redirect: "/resources/sample",component: () => import("../views/resources/resources.vue"),children: [{path: "/resources/sample",name: "sample",component: () => import("../views/resources/components/sample.vue"),},],},{path: "/news",name: "news",component: () => import("../views/news/news.vue"),},{path: "/about",name: "about",component: () => import("../views/about/aboutus.vue"),},{path: "/contactus",name: "contactus",component: () => import("../views/contactus/contactus.vue"),},],},
];const router = createRouter({history: createWebHashHistory(),routes,
});let startTime = Date.now();
localStorage.setItem("lastPageTime", startTime);
let currentTime;
const _routeObj = {"/content": "首页","/services/0": "NGS- Whole Genome Sequencing","/services/1": "NGS- mRNA Sequencing","/services/2": "NGS- Small RNA Sequencing","/services/3": "NGS-Lnc RNA Sequencing","/services/4": "NGS- Metagenome Sequencing","/services/5": "NGS- Microbial diversity Sequencing","/services/6": "NGS- Hi-C Sequencing","/services/7": "Nanopore- Nanopore Ultra-long Sequencing","/services/8": "Nanopore- Nanopore Sequencing","/services/9": "Nanopore- Direct RNA Sequencing","/services/10": "Nanopore- Lnc RNA Sequencing","/services/11": "Nanopore- CircRNA Sequencing","/services/12": "Nanopore- TAIL Iso Sequencing","/services/13": "Nanopore- Isoform Sequencing","/services/14": "Nanopore- Direct-CDNA Sequencing","/services/15": "Nanopore- Single-cell full-length transcriptome Sequencing","/services/16": "Nanopore- Full-length 16S/18S/ITS Amplicon Sequencing","/services/17": "Nanopore- 16S-23S Amplicon Metagenomic Sequencing","/services/18": "Nanopore- Metagenome Sequencing","/services/19": "Nanopore- PORE-C Sequencing","/services/20": "Pacbio Revio- Revio Sequencing","/resources/sample": "资源-样本要求","/about": "关于我们","/contactus": "联系我们",
};router.beforeEach((to, from) => {// console.log(to, from);if (to && _routeObj[from.path]) {// 第一步:页面跳转后记录一下当前的时间 currentTimecurrentTime = Date.now();// 第二步:通过计算currentTime - startTime 的 差值 之后,再上报数据let _formdata = {};_formdata.visitorId = localStorage.getItem("userId");_formdata.url = from.path;_formdata.classify = _routeObj[from.path];_formdata.time = currentTime - startTime;// console.log(_formdata);// debugger;DataburialpointApi(_formdata).then((res) => {console.log(res);}).catch((err) => {console.log(err);});// 第三步:每次都要初始化一下 startTimestartTime = Date.now();localStorage.setItem("lastPageTime", startTime);}
});
export default router;

注意:
上面的 localStorage.setItem(“lastPageTime”, startTime); 是后续做 解决监听不到关闭网页的问题 的时候加上的!

解决监听不到关闭网页的问题

在app.vue里面加上监听页面关闭事件,这个必须用原生js,vue 的 onBeforeUnmount 根本不能在界面标签关闭时做操作!

import { useMainStore } from "@/store";
import { useRoute } from "vue-router";
// 路由菜单相关
let route = useRoute();
const mainStore = useMainStore();
onMounted(() => {// 监听页面关闭事件window.addEventListener("beforeunload", function (event) {// 计算页面停留时间(以毫秒为单位)const stayTime = new Date() - localStorage.getItem("lastPageTime");let _formdata = {};_formdata.visitorId = localStorage.getItem("userId");_formdata.url = route.path;_formdata.classify = mainStore.routeObj[route.path];_formdata.time = stayTime;// console.log(_formdata);// debugger;DataburialpointApi(_formdata).then((res) => {console.log(res);}).catch((err) => {console.log(err);});// 确保关闭页面时不会阻止默认行为delete event["returnValue"];});
});

注意:
这里 mainStore.routeObj 和上面的 _routeObj 是同一个东西,但是不能直接在 route/index.js 中使用 pinia,会报错,搜索是因为在 pinia 挂载之前使用了,具体怎么解决不知道!!!望读者 指点江山,激扬文字!!!

三、按钮埋点

到这里,菜鸟的需求就结束了,但是菜鸟拓展了一下,其实这个也很简单,就是每个按钮点击请求的时候,让后端统计一下,或者前端请求一下后端的某个接口就行!当然如果能直接封装成 自定义指令 就最好了!!!

参考文章:
vue项目进行前端埋点,记录页面停留时间

这篇关于vue 数据埋点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/875259

相关文章

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

Oracle数据库使用 listagg去重删除重复数据的方法汇总

《Oracle数据库使用listagg去重删除重复数据的方法汇总》文章介绍了在Oracle数据库中使用LISTAGG和XMLAGG函数进行字符串聚合并去重的方法,包括去重聚合、使用XML解析和CLO... 目录案例表第一种:使用wm_concat() + distinct去重聚合第二种:使用listagg,

Python实现将实体类列表数据导出到Excel文件

《Python实现将实体类列表数据导出到Excel文件》在数据处理和报告生成中,将实体类的列表数据导出到Excel文件是一项常见任务,Python提供了多种库来实现这一目标,下面就来跟随小编一起学习一... 目录一、环境准备二、定义实体类三、创建实体类列表四、将实体类列表转换为DataFrame五、导出Da

Python实现数据清洗的18种方法

《Python实现数据清洗的18种方法》本文主要介绍了Python实现数据清洗的18种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1. 去除字符串两边空格2. 转换数据类型3. 大小写转换4. 移除列表中的重复元素5. 快速统

Python数据处理之导入导出Excel数据方式

《Python数据处理之导入导出Excel数据方式》Python是Excel数据处理的绝佳工具,通过Pandas和Openpyxl等库可以实现数据的导入、导出和自动化处理,从基础的数据读取和清洗到复杂... 目录python导入导出Excel数据开启数据之旅:为什么Python是Excel数据处理的最佳拍档

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

在Pandas中进行数据重命名的方法示例

《在Pandas中进行数据重命名的方法示例》Pandas作为Python中最流行的数据处理库,提供了强大的数据操作功能,其中数据重命名是常见且基础的操作之一,本文将通过简洁明了的讲解和丰富的代码示例,... 目录一、引言二、Pandas rename方法简介三、列名重命名3.1 使用字典进行列名重命名3.编