Commit 15b127dc by 杨明

发送

1 parent 70abccb1
...@@ -2,16 +2,17 @@ ...@@ -2,16 +2,17 @@
* @Author: yangming yangming@didihu.com.cn * @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-24 17:28:02 * @Date: 2023-11-24 17:28:02
* @LastEditors: yangming yangming@didihu.com.cn * @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-21 13:50:59 * @LastEditTime: 2023-12-22 10:52:37
* @FilePath: \rollup_demo\src\main.ts * @FilePath: \rollup_demo\src\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
// import Request from './modules/request' // import Request from './modules/request'
import type { BaseOptions, trackType } from './types/main' import type { BaseOptions, ClickOption, trackType, ScrollOption } from './types/main'
import stayEvent from './modules/stayEvent' import stayEvent from './modules/stayEvent'
import clickEvent from './modules/clickEvent' import clickEvent from './modules/clickEvent'
import viewEvent from './modules/viewEvent' import viewEvent from './modules/viewEvent'
import report from './modules/report'
import User from './modules/user' import User from './modules/user'
// const request = new Request('https://www.xxx.com', 'ajax') // const request = new Request('https://www.xxx.com', 'ajax')
class Tracker extends User { class Tracker extends User {
...@@ -35,6 +36,15 @@ class Tracker extends User { ...@@ -35,6 +36,15 @@ class Tracker extends User {
open_all_click: true, open_all_click: true,
collect_doms: ['p'] collect_doms: ['p']
}, },
reportConfig: {
send_count: 2,
send_time: 10,
switch_start: true,
post_way: 'ajax', // ajax image
server_url: 'http://192.168.1.126:3000/log',
server_img_url: 'http://192.168.1.126:3000/read-image/log.gif',
track_data_name: 'track_data'
},
scrollview: { scrollview: {
collect_url: function () { collect_url: function () {
return true return true
...@@ -48,9 +58,10 @@ class Tracker extends User { ...@@ -48,9 +58,10 @@ class Tracker extends User {
* 初始化数据 * 初始化数据
*/ */
init() { init() {
stayEvent.init(this.SDK.scrollview) stayEvent.init(this.SDK.scrollview as ScrollOption)
clickEvent.init(this.SDK.clickview) clickEvent.init(this.SDK.clickview as ClickOption)
viewEvent.init(this.SDK.router_type) viewEvent.init(this.SDK.router_type)
report.init(this.SDK.reportConfig)
} }
/** /**
* 代码埋点触发元素点击事件 * 代码埋点触发元素点击事件
......
...@@ -2,7 +2,15 @@ ...@@ -2,7 +2,15 @@
* @Author: yangming yangming@didihu.com.cn * @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:10:56 * @Date: 2023-11-30 14:10:56
* @LastEditors: yangming yangming@didihu.com.cn * @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-21 09:20:19 * @LastEditTime: 2023-12-21 18:16:19
* @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:10:56
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-21 18:13:19
* @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts * @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEupo * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEupo
*/ */
...@@ -11,8 +19,31 @@ import utils from '../utils/helper' ...@@ -11,8 +19,31 @@ import utils from '../utils/helper'
import type { ClickDescriptor, ElDescriptor, ClickParmasDescriptor } from '../types/click' import type { ClickDescriptor, ElDescriptor, ClickParmasDescriptor } from '../types/click'
import { TAG_PROP_NAME, TAGS } from '../utils/constants' import { TAG_PROP_NAME, TAGS } from '../utils/constants'
import collect from './collect' import collect from './collect'
import { ClickOption } from '../types/main'
class clickEvent extends Events { class clickEvent extends Events {
_options: any _options!: ClickOption
/**
* 初始化,并绑定监听事件
* params data配置信息
*/
init<T>(this: clickEvent, data: ClickOption) {
this._options = data
if (this._options.open_all_click) {
this.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
let _target = e.target as HTMLElement
this.start({ event: e, target: _target, tagName: _target.tagName.toLowerCase() })
})
} else {
this.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
let target = e.target as HTMLElement
let _target = this.getTargetElement(target)
if (_target) {
this.start({ event: e, target: _target, tagName: _target.tagName.toLowerCase() })
}
})
}
}
/** /**
* 获取元素所有信息数据 * 获取元素所有信息数据
* @param target 接收的数据 * @param target 接收的数据
...@@ -130,27 +161,6 @@ class clickEvent extends Events { ...@@ -130,27 +161,6 @@ class clickEvent extends Events {
} }
} }
/** /**
* 初始化,并绑定监听事件
* params 配置信息
*/
init<T>(this: clickEvent, data: T) {
this._options = data
if (this._options.open_all_click) {
this.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
let _target = e.target as HTMLElement
this.start({ event: e, target: _target, tagName: _target.tagName.toLowerCase() })
})
} else {
this.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
let target = e.target as HTMLElement
let _target = this.getTargetElement(target)
if (_target) {
this.start({ event: e, target: _target, tagName: _target.tagName.toLowerCase() })
}
})
}
}
/**
* 开始收集数据 * 开始收集数据
* params 配置信息 * params 配置信息
*/ */
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
* @Author: yangming yangming@didihu.com.cn * @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 17:05:34 * @Date: 2023-11-28 17:05:34
* @LastEditors: yangming yangming@didihu.com.cn * @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-21 09:28:02 * @LastEditTime: 2023-12-21 15:39:21
* @FilePath: \glsx-tracker-javascript\src\modules\collect.ts * @FilePath: \glsx-tracker-javascript\src\modules\collect.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
import type { DefaultInfoDescriptor, EventType, ReportInfoDescriptor } from '../types/collect' import type { DefaultInfoDescriptor, EventType, ReportInfoDescriptor } from '../types/collect'
import utils from '../utils/helper' import utils from '../utils/helper'
import { EVENT_TYPE } from '../utils/constants' import { EVENT_TYPE } from '../utils/constants'
import _localStorage from './loggerAndStorage'
class Collect { class Collect {
getUUID: string getUUID: string
user_login_id?: string | number user_login_id?: string | number
...@@ -60,7 +61,7 @@ class Collect { ...@@ -60,7 +61,7 @@ class Collect {
report_time: utils.dateFormat(Date.now()), report_time: utils.dateFormat(Date.now()),
anonymous_id: this.getUUID anonymous_id: this.getUUID
} }
console.log(reportInfo) _localStorage.setTrack(reportInfo)
} }
/** /**
* 发送埋点数据 * 发送埋点数据
...@@ -76,6 +77,7 @@ class Collect { ...@@ -76,6 +77,7 @@ class Collect {
reportInfo.user_login_id = this.user_login_id reportInfo.user_login_id = this.user_login_id
} }
console.log(reportInfo) console.log(reportInfo)
_localStorage.setTrack(reportInfo)
} }
} }
export default new Collect() export default new Collect()
...@@ -51,8 +51,9 @@ let _localStorage = { ...@@ -51,8 +51,9 @@ let _localStorage = {
return supported return supported
}, },
setTrack: function (value: any): string | undefined | void { setTrack: function (value: any): string | undefined | void {
console.log(88888)
try { try {
if (!DEFAULT_SETTING.switch_start) return logger.log('tracker is closed') // if (!DEFAULT_SETTING.switch_start) return logger.log('tracker is closed')
let _temp: UploadTrackDescriptor = { let _temp: UploadTrackDescriptor = {
createTrackDate: new Date().getTime(), createTrackDate: new Date().getTime(),
data: [], data: [],
......
import storage from './loggerAndStorage.js'
import http from './request.js'
import Util from '../utils/helper.js'
import type { ReportOption } from '../types/report.js'
// 异步发送策略
class sendStrategy {
dataHasSend = true
dataHasChange = false
syncStorage = false
failTime = 0
is_first_batch_write = false
DEFAULT_SETTING!: ReportOption
init(options: ReportOption) {
this.DEFAULT_SETTING = options
if (this.DEFAULT_SETTING.switch_start) {
this.dataHasChange = true
this.is_first_batch_write = true
this.batchInterval()
}
}
batchInterval() {
const _this = this
function loopWrite() {
setTimeout(() => {
_this.batchWrite()
loopWrite()
}, 500)
}
function loopSend() {
setTimeout(() => {
_this.batchSend()
loopSend()
}, 1000 * _this.DEFAULT_SETTING.send_time * Math.pow(2, _this.failTime))
}
loopWrite()
loopSend()
}
batchWrite() {
const _this = this
if (this.dataHasChange) {
if (this.is_first_batch_write) {
this.is_first_batch_write = false
setTimeout(() => {
_this.batchSend()
}, 1000)
}
this.dataHasChange = false
}
}
batchSend(singeleID?: number) {
if (this.dataHasSend) {
let data = []
if (singeleID) {
data = storage.getTrack(0, true)
data = data.filter(item => item.id == singeleID)
} else {
data = storage.getTrack(this.DEFAULT_SETTING.send_count, true)
console.log(data, 'data')
}
if (data.length > 0) {
this.dataHasSend = false
this.webrequest({
data,
success: this.batchRemove.bind(this),
fail: this.sendFail.bind(this)
})
}
}
}
batchRemove(data: any) {
// 删除数据
storage.removeTrack(data)
this.dataHasSend = true
this.dataHasChange = true
this.batchWrite()
this.failTime = 0
}
sendFail(data: any) {
this.dataHasSend = true
this.failTime++
storage.removeUploadData(data)
}
webrequest(option: { data: any; success: any; fail: any }) {
if (option.data.length > 0) {
console.log(option.data, new Date())
if (this.DEFAULT_SETTING.post_way == 'image') {
const upload_data = Util.handlerTrackData(option.data)
storage.uploadTrack(option.data)
const base_url = this.DEFAULT_SETTING.server_img_url
const param = JSON.stringify(upload_data)
const date = new Date().getTime()
try {
http.imagehttp.server_url = `${base_url}?d=${date}&data=${param}`
http.imagehttp.successCallBack = function () {
option.success(option.data)
console.log('发送成功', new Date())
}
let _this = this
http.imagehttp.errorCallback = function () {
console.log('发送失败', new Date())
option.fail(option.data)
_this.useAjax(option)
}
http.imagehttp.start()
} catch (error) {
console.log(error)
}
} else if (this.DEFAULT_SETTING.post_way == 'ajax') {
this.useAjax(option)
}
}
}
send(data: any) {
if (data && this.DEFAULT_SETTING.switch_start) {
this.dataHasChange = true
const id = storage.setTrack(data)
this.batchSend(id as unknown as number)
}
}
useAjax(option: { data: any; success: any; fail: any }) {
const url = this.DEFAULT_SETTING.server_url
const upload_data = Util.handlerTrackData(option.data)
storage.uploadTrack(option.data)
const param = JSON.stringify(upload_data)
let _this = this
http.ajax({
url,
type: 'POST',
data: { data: param },
success: function (res) {
option.success(option.data)
},
error: function (err) {
option.fail(option.data)
_this.useBeacon(option)
}
})
}
useBeacon(option: { data: any; success: any; fail?: any }) {
const url = this.DEFAULT_SETTING.server_url
const upload_data = Util.handlerTrackData(option.data)
storage.uploadTrack(option.data)
const param = JSON.stringify(upload_data)
const blob = new Blob([param], { type: 'application/json' })
http.beason.server_url = url
http.beason.start(blob)
http.beason.callback = function () {
option.success(option.data)
}
}
}
export default new sendStrategy()
...@@ -55,8 +55,8 @@ function ajax(params: AjaxParams): void { ...@@ -55,8 +55,8 @@ function ajax(params: AjaxParams): void {
} }
class BeaconSend { class BeaconSend {
private callback: (() => void) | null = null public callback: (() => void) | null = null
private server_url: string = '' public server_url: string = ''
start(data: any): void { start(data: any): void {
if (!this.server_url) return if (!this.server_url) return
...@@ -77,12 +77,12 @@ class BeaconSend { ...@@ -77,12 +77,12 @@ class BeaconSend {
} }
class ImageSend { class ImageSend {
private errorCallback: (() => void) | null = null public errorCallback: (() => void) | null = null
private img: HTMLImageElement = document.createElement('img') public img: HTMLImageElement = document.createElement('img')
private server_url: string = '' public server_url: string = ''
private datasend_timeout: number = 3000 public datasend_timeout: number = 3000
private ignore_oom: boolean = true public ignore_oom: boolean = true
private successCallBack: (() => void) | null = null public successCallBack: (() => void) | null = null
constructor() { constructor() {
this.img.width = 1 this.img.width = 1
......
import storage from './loggerAndStorage' import storage from './loggerAndStorage'
class AsyncSendStrategy { class AsyncSendStrategy {
private callback: (() => void) | null = null public callback: (() => void) | null = null
private sendTime: number = 1000 * 5 public sendTime: number = 1000 * 5
private sendNum: number = 10 public sendNum: number = 10
private dataHasSend: boolean = true public dataHasSend: boolean = true
private dataHasChange: boolean = false public dataHasChange: boolean = false
private syncStorage: boolean = false public syncStorage: boolean = false
private failTime: number = 0 public failTime: number = 0
private is_first_batch_write: boolean = true public is_first_batch_write: boolean = true
start(): void { start(): void {
this.batchInterval() this.batchInterval()
} }
private batchInterval(): void { public batchInterval(): void {
const _this = this const _this = this
function loopWrite(): void { function loopWrite(): void {
...@@ -35,7 +35,7 @@ class AsyncSendStrategy { ...@@ -35,7 +35,7 @@ class AsyncSendStrategy {
loopSend() loopSend()
} }
private batchWrite(): void { public batchWrite(): void {
const _this = this const _this = this
if (this.dataHasChange) { if (this.dataHasChange) {
...@@ -56,7 +56,7 @@ class AsyncSendStrategy { ...@@ -56,7 +56,7 @@ class AsyncSendStrategy {
} }
} }
private batchSend(): void { public batchSend(): void {
if (this.dataHasSend) { if (this.dataHasSend) {
let data let data
let len let len
...@@ -82,21 +82,21 @@ class AsyncSendStrategy { ...@@ -82,21 +82,21 @@ class AsyncSendStrategy {
} }
} }
private batchRemove(len: number): void { public batchRemove(len: number): void {
// 删除数据 // 删除数据
storage.removeTrack() storage.removeTrack(undefined)
this.dataHasSend = true this.dataHasSend = true
this.dataHasChange = true this.dataHasChange = true
this.batchWrite() this.batchWrite()
this.failTime = 0 this.failTime = 0
} }
private sendFail(): void { public sendFail(): void {
this.dataHasSend = true this.dataHasSend = true
this.failTime++ this.failTime++
} }
private webRequest(option: { data: any; len: number; success: (len: number) => void; fail: () => void }): void { public webRequest(option: { data: any; len: number; success: (len: number) => void; fail: () => void }): void {
// if (option.data.length > 0) { // if (option.data.length > 0) {
// const data = kit.batchTrackData(option.data); // const data = kit.batchTrackData(option.data);
// logger.info('上报数据监控:', decodeURIComponent(data)); // logger.info('上报数据监控:', decodeURIComponent(data));
......
...@@ -2,16 +2,17 @@ ...@@ -2,16 +2,17 @@
* @Author: yangming yangming@didihu.com.cn * @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:46:13 * @Date: 2023-11-30 14:46:13
* @LastEditors: yangming yangming@didihu.com.cn * @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-20 21:16:49 * @LastEditTime: 2023-12-22 10:48:18
* @FilePath: \glsx-tracker-javascript\src\modules\stayEvent.ts * @FilePath: \glsx-tracker-javascript\src\modules\stayEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
import { ScrollOption } from '../types/main'
import type { StayDescriptor } from '../types/stay' import type { StayDescriptor } from '../types/stay'
import collect from './collect' import collect from './collect'
import Events from './events' import Events from './events'
class StayEvent extends Events { class StayEvent extends Events {
static instance: StayEvent | undefined static instance: StayEvent | undefined
_options: any _options!: ScrollOption
start_time: number = Date.now() start_time: number = Date.now()
timeout_time: number = 1000 timeout_time: number = 1000
timer: NodeJS.Timeout | undefined timer: NodeJS.Timeout | undefined
...@@ -19,14 +20,14 @@ class StayEvent extends Events { ...@@ -19,14 +20,14 @@ class StayEvent extends Events {
/** /**
* 获取视口位置 * 获取视口位置
*/ */
getPosition(): number { get getPosition(): number {
let position = document.documentElement.scrollTop || document.body.scrollTop let position = document.documentElement.scrollTop || document.body.scrollTop
return +position.toFixed(2) return +position.toFixed(2)
} }
/** /**
* 获取停留时间 * 获取停留时间
*/ */
getStayTime(): number { get getStayTime(): number {
let duration = (Date.now() - this.start_time) / 1000 let duration = (Date.now() - this.start_time) / 1000
if (duration < 0) { if (duration < 0) {
duration = 0 duration = 0
...@@ -36,9 +37,9 @@ class StayEvent extends Events { ...@@ -36,9 +37,9 @@ class StayEvent extends Events {
/** /**
* 获取停留事件的数据 * 获取停留事件的数据
*/ */
getStayInfo(): StayDescriptor { get getStayInfo(): StayDescriptor {
const position = this.getPosition() const position = this.getPosition
const duration = this.getStayTime() const duration = this.getStayTime
return { return {
position, position,
duration duration
...@@ -57,7 +58,7 @@ class StayEvent extends Events { ...@@ -57,7 +58,7 @@ class StayEvent extends Events {
return false return false
} }
if (!this.timer) { if (!this.timer) {
const para = this.getStayInfo() const para = this.getStayInfo
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.send(para) this.send(para)
}, this.timeout_time) }, this.timeout_time)
...@@ -76,7 +77,7 @@ class StayEvent extends Events { ...@@ -76,7 +77,7 @@ class StayEvent extends Events {
* 初始化 * 初始化
* params data初始化数据 * params data初始化数据
*/ */
init<T>(data: T) { init<T>(data: ScrollOption) {
this._options = data this._options = data
this.trigger() this.trigger()
} }
......
...@@ -18,4 +18,4 @@ export interface ElDescriptor { ...@@ -18,4 +18,4 @@ export interface ElDescriptor {
element_id?: string element_id?: string
class_name?: string class_name?: string
element_content: string element_content: string
} }
\ No newline at end of file \ No newline at end of file
...@@ -4,7 +4,7 @@ import { routerOption } from './view' ...@@ -4,7 +4,7 @@ import { routerOption } from './view'
* @Author: yangming yangming@didihu.com.cn * @Author: yangming yangming@didihu.com.cn
* @Date: 2023-12-12 14:35:34 * @Date: 2023-12-12 14:35:34
* @LastEditors: yangming yangming@didihu.com.cn * @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-20 20:01:38 * @LastEditTime: 2023-12-21 17:15:51
* @FilePath: \glsx-tracker-javascript\src\types\main.ts * @FilePath: \glsx-tracker-javascript\src\types\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
...@@ -33,5 +33,6 @@ export type optonsDescriptor = { ...@@ -33,5 +33,6 @@ export type optonsDescriptor = {
is_encryption: boolean is_encryption: boolean
clickview: ClickOption clickview: ClickOption
scrollview: ScrollOption scrollview: ScrollOption
reportConfig:any
} }
export type BaseOptions = Partial<optonsDescriptor> export type BaseOptions = Partial<optonsDescriptor>
export interface ReportOption {
send_count: number
send_time: number
switch_start: boolean
post_way: string // ajax image
server_url: string
server_img_url: string
track_data_name: string
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!