Commit dd741db0 by 杨明

浏览事件

1 parent a2f1a855
......@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-24 17:29:53
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 17:53:43
* @LastEditTime: 2023-12-14 17:55:38
* @FilePath: \rollup_demo\public\index.html
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
......@@ -25,20 +25,83 @@
<div>
<button class="class" name="name" id="id">111</button>
</div>
<script src="../dist/glsx-tracker-javascript.umd.js"></script>
<script>
const tracker = new Tracker()
document.querySelector('.test').addEventListener('click', function (e) {
tracker.track(
this,
{
aaa: 999
},
function () {
console.log('t')
}
)
})
</script>
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- use router-link component for navigation. -->
<!-- specify the link by passing the `to` prop. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
<button>点击</button>
<div class="aaa"><div id="testp">44</div></div>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/vue-router/3.1.0/vue-router.min.js"></script>
<script src="../dist/glsx-tracker-javascript.umd.js"></script>
<script>
console.log(8888)
const tracker = new Tracker()
document.querySelector('.test').addEventListener('click', function (e) {
tracker.track(
this,
{
aaa: 999
},
function () {
console.log('t')
}
)
})
</script>
<script>
const Foo = { template: "<div>foo</div>" };
const Bar = { template: "<div>bar</div>" };
// 2. Define some routes
// Each route should map to a component. The "component" can
// either be an actual component constructor created via
// `Vue.extend()`, or just a component options object.
// We'll talk about nested routes later.
const routes = [
{ path: "/foo", component: Foo },
{ path: "/bar", component: Bar },
];
// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = new VueRouter({
routes, // short for `routes: routes`
});
router.beforeEach(function (to, from, next) {
//console.log('url1:', document.URL);
setTimeout(function () {
//console.log('url2:', document.URL);
// tracker.autoTriggerView({
// aa:11
// })
next();
}, 0);
});
const app = new Vue({
router,
}).$mount("#app");
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("get", "https://www.czhuihao.cn/ip/", false);
xmlhttp.send();
var item = JSON.parse(xmlhttp.responseText);
console.log(item)
</script>
</body>
</html>
......@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-24 17:28:02
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 17:37:25
* @LastEditTime: 2023-12-14 10:30:33
* @FilePath: \rollup_demo\src\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -15,6 +15,9 @@ import viewEvent from './modules/viewEvent'
const request = new Request('https://www.xxx.com', 'ajax')
class Tracker {
SDK: BaseOptions
/**
* SDK配置
*/
constructor(options: BaseOptions) {
this.SDK = Object.assign(this.initOptions(), options)
this.init()
......@@ -25,7 +28,7 @@ class Tracker {
initOptions(): BaseOptions {
return {
track_type: 'image',
router_type:'hash',
router_type: 'hash',
clickview: {
open_all_click: false,
collect_doms: ['p']
......@@ -53,6 +56,9 @@ class Tracker {
track(target: HTMLElement, prop: any, callback: Function) {
clickEvent.activeTrigger.call(clickEvent, target, prop, callback)
}
autoTriggerView(prop: any) {
viewEvent.trigger.call(viewEvent, prop)
}
/**
* 设置上报公共属性
*/
......
......@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:10:56
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 15:23:42
* @LastEditTime: 2023-12-14 17:38:12
* @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEupo
*/
......@@ -11,6 +11,7 @@ import utils from '../utils/helper'
const TAGS = ['a', 'input', 'textarea', 'button']
import type { ClickDescriptor, ElDescriptor, ClickParmasDescriptor } from '../types/click'
import { TAG_PROP_NAME } from '../utils/constants'
import collect from './collect'
class clickEvent {
_options: any
/**
......@@ -131,11 +132,11 @@ class clickEvent {
init<T>(this: clickEvent, data: T) {
this._options = data
if (this._options.open_all_click) {
Events.addEvent(document.documentElement, 'click', (e: MouseEvent)=> {
Events.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
console.log(this.getEleDetail(e.target as HTMLElement))
})
} else {
Events.addEvent(document.documentElement, 'click', (e?: MouseEvent)=> {
Events.addEvent(document.documentElement, 'click', (e?: MouseEvent) => {
console.log(e)
if (!e) {
return false
......@@ -149,11 +150,11 @@ class clickEvent {
}
}
start({ event, target, tagName, customProps, callback }: ClickParmasDescriptor) {
if(typeof callback == 'function') {
if (typeof callback == 'function') {
callback.call(null)
}
let para = this.getBasicEleInfo({ event, target, tagName, customProps })
console.log(para)
collect.getClickEvent(para.props)
}
}
export default new clickEvent()
......@@ -2,11 +2,12 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 17:05:34
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 10:35:54
* @LastEditTime: 2023-12-15 09:35:50
* @FilePath: \glsx-tracker-javascript\src\modules\collect.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import type { DefaultInfoDescriptor, EventType } from '../types/collect'
import type { DefaultInfoDescriptor, EventType ,DeviceInfoDescriptor} from '../types/collect'
import utils from '../utils/helper'
class Collect {
/**
*
......@@ -23,10 +24,21 @@ class Collect {
*/
getUserData() {}
/**
* 采集公共信息数据
* 采集设备信息数据
* @param info 接收的数据
*/
getDeviceData():DeviceInfoDescriptor {
return {
$plateform: navigator.platform,
$appName: navigator.appName,
$userAgent: navigator.userAgent,
$appVersion: navigator.appVersion
}
}
/**
* 采集默认信息数据
* @param info 接收的数据
*/
getCommonData() {}
getDefaultData(): DefaultInfoDescriptor {
return {
$page_title: document.title,
......@@ -38,26 +50,32 @@ class Collect {
* 收集点击事件数据
* 判断需要监听哪些dom元素
*/
getClickEvent() {}
getClickEvent<T extends object>(para: T) {
this.send(para, 'clickEvent')
}
/**
* 收集浏览事件数据
*/
getViewEvent() {}
getViewEvent<T extends object>(para: T) {
this.send(para, 'viewEvent')
}
/**
* 收集停留事件数据
*/
getStayEvent<T extends object>(para: T) {
this.send(Object.assign(this.getDefaultData(), para), 'stayEvent')
}
send<T>(para: T, type: EventType) {
console.log(para, type)
this.send(para, 'stayEvent')
}
/**
* 自定义事件收集数据
* @param target 目标元素
* @param props 属性
* @param callback 回调函数
* 发送埋点数据
*/
trackHeatMap() {}
send<T>(para: T, type: EventType) {
para = Object.assign(this.getDefaultData(), para)
const reportInfo = {
properties: para,
event: type,
time: utils.dateFormat(Date.now())
}
console.log(reportInfo)
}
}
export default new Collect()
......@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 17:15:38
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-12 15:47:28
* @LastEditTime: 2023-12-14 14:58:34
* @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -28,7 +28,6 @@ class Events {
element.addEventListener(
type,
e => {
console.log(8888)
handler.call(this, e)
},
useCapture
......
......@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:46:13
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 15:37:25
* @LastEditTime: 2023-12-14 16:04:28
* @FilePath: \glsx-tracker-javascript\src\modules\stayEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -11,7 +11,7 @@ import collect from './collect'
import Events from './events'
class StayEvent {
_options: any
start_time: number = 0
start_time: number = Date.now()
timeout_time: number = 1000
timer: NodeJS.Timeout | undefined
interval_time: number = 5000
......@@ -52,9 +52,13 @@ class StayEvent {
if (this._options.collect_url && !this._options.collect_url()) {
return false
}
if(Date.now() - this.start_time<this._options.delay_time) {
return false
}
if (!this.timer) {
const para = this.getStayInfo()
this.timer = setTimeout(()=> {
this.send()
this.send(para)
}, this.timeout_time)
}
})
......@@ -62,9 +66,9 @@ class StayEvent {
/**
* 发送数据
*/
send() {
send(para:object) {
this.timer = undefined
const para = this.getStayInfo()
this.start_time = Date.now()
collect.getStayEvent(para)
}
/**
......
......@@ -2,26 +2,36 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:45:53
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 17:57:01
* @LastEditTime: 2023-12-14 17:36:55
* @FilePath: \glsx-tracker-javascript\src\modules\viewEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import type { viewBase, routerOption } from '../types/view'
import Events from './events'
import utils from '../utils/helper'
import collect from './collect'
class ViewEvent {
baseParams: viewBase
last_time:string
routerModel: routerOption | undefined = 'hash'
/**
* 初始化页面信息
*$page_title: 页面标题
$page_url: 页面地址
$page_path: 页面地址
$path_href: 页面完整地址
$enter_time: 进入页面的时间
$leave_time:离开页面的时间
$page_referrer: 页面来源
*/
initPage(): viewBase {
return {
constructor() {
this.last_time = utils.dateFormat(new Date())
this.baseParams = {
$page_title: document.title,
$page_url: location.href,
$page_path: location.pathname,
$enter_time: this.last_time,
$leave_time: '',
$page_referrer: ''
}
}
......@@ -30,15 +40,14 @@ class ViewEvent {
*/
init(routerModel?: routerOption) {
this.routerModel = routerModel
this.initPage()
this.initEvent()
}
/**
* 重写pushState/replaceState事件
*/
initEvent() {
window.history['pushState'] = this.createHistoryEvnent('pushState')
window.history['replaceState'] = this.createHistoryEvnent('replaceState')
window.history['pushState'] = this.createHistoryEvent('pushState')
window.history['replaceState'] = this.createHistoryEvent('replaceState')
if (this.routerModel == 'history') {
this.captureEvents(['pushState', 'replaceState', 'popstate'])
}
......@@ -46,31 +55,50 @@ class ViewEvent {
this.captureEvents(['hashchange'])
}
}
/**
* 绑定hashchange、popstate事件
*/
captureEvents<T>(MouseEventList: string[]) {
MouseEventList.forEach(event => {
Events.addEvent(window, event, (e: MouseEvent) => {
console.log(555)
this.trigger()
})
})
}
createHistoryEvnent = <T extends keyof History>(type: T): (() => any) => {
/**
* 创建事件
* params history事件名
*/
createHistoryEvent = <T extends keyof History>(type: T): (() => any) => {
const origin = history[type]
const self = this
return function (this: any) {
const res = origin.apply(this, arguments)
var e = new Event(type)
window.dispatchEvent(e)
self.trigger()
return res
}
}
/**
* 获取页面信息
* @param target 接收的数据
* 修改页面信息、并合并自定义属性
* @param customProps 自定义属性
*/
getPresetProperties() {}
getPresetProperties(customProps: any) {
const current_url = location.href
this.baseParams.$page_referrer = this.baseParams.$page_url
this.baseParams.$page_url = current_url
const current_time = utils.dateFormat(new Date())
this.baseParams.$leave_time = current_time
this.baseParams.$enter_time = this.last_time;
this.last_time = current_time
let _customProps = typeof customProps === 'object' && customProps !== null ? customProps : {}
return Object.assign(this.baseParams, _customProps)
}
/**
* 绑定浏览事件
*/
trigger() {}
trigger(prop?: any) {
const para = this.getPresetProperties(prop)
collect.getViewEvent(para)
}
}
export default new ViewEvent()
......@@ -11,3 +11,10 @@ export interface DefaultInfoDescriptor {
$page_url: string | undefined
$page_path: string | undefined
}
export interface DeviceInfoDescriptor {
$plateform: string
$appName: string
$userAgent: string
$appVersion: string
[prop: string]: string
}
......@@ -4,7 +4,7 @@ import { routerOption } from './view'
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-12-12 14:35:34
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 17:36:03
* @LastEditTime: 2023-12-15 09:21:40
* @FilePath: \glsx-tracker-javascript\src\types\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -18,6 +18,7 @@ export type ScrollOption = {
delay_time: number
}
export type optonsDescriptor = {
SDK_version: string
server_url: string
router_type: routerOption
track_type: trackType
......
......@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-12-11 16:15:11
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-11 16:18:57
* @LastEditTime: 2023-12-14 14:33:28
* @FilePath: \glsx-tracker-javascript\src\types\view.d.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -10,6 +10,8 @@ export interface viewBase {
$page_title: string
$page_url: string
$page_path: string
$enter_time:string
$leave_time:string
$page_referrer: string | undefined
}
export type routerOption = 'history' | 'hash'
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!