Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
npm
/
glsx-tracker-javascript
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 59aff8a5
authored
Dec 21, 2023
by
杨明
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
登录
1 parent
7cf9845a
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
160 additions
and
84 deletions
public/index.html
src/main.ts
src/modules/clickEvent.ts
src/modules/collect.ts
src/modules/events.ts
src/modules/stayEvent.ts
src/modules/user.ts
src/modules/viewEvent.ts
src/types/collect.d.ts
src/types/main.ts
src/types/user.d.ts
src/utils/constants.ts
public/index.html
View file @
59aff8a
...
...
@@ -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-
15 16:51:23
* @LastEditTime: 2023-12-
20 22:26:08
* @FilePath: \rollup_demo\public\index.html
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
...
...
@@ -44,8 +44,12 @@
<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
()
tracker
.
setAttribute
({
phone
:
19999999
})
tracker
.
login
(
'18888888'
)
tracker
.
setCommonAttribute
({
username
:
'444'
})
document
.
querySelector
(
'.test'
).
addEventListener
(
'click'
,
function
(
e
)
{
tracker
.
track
(
this
,
...
...
src/main.ts
View file @
59aff8a
...
...
@@ -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-
15 16:49:04
* @LastEditTime: 2023-12-
20 22:15:08
* @FilePath: \rollup_demo\src\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
...
...
@@ -31,7 +31,7 @@ class Tracker {
track_type
:
'image'
,
router_type
:
'hash'
,
clickview
:
{
open_all_click
:
fals
e
,
open_all_click
:
tru
e
,
collect_doms
:
[
'p'
]
},
scrollview
:
{
...
...
@@ -57,13 +57,18 @@ class Tracker {
track
(
target
:
HTMLElement
,
prop
:
any
,
callback
:
Function
)
{
clickEvent
.
activeTrigger
.
call
(
clickEvent
,
target
,
prop
,
callback
)
}
/**
* 手动触发路由事件
*/
autoTriggerView
(
prop
:
any
)
{
viewEvent
.
trigger
.
call
(
viewEvent
,
prop
)
}
/**
*
设置
上报公共属性
*
添加
上报公共属性
*/
setCommonAttribute
()
{}
setCommonAttribute
(
obj
:
object
)
{
user
.
addCommonData
(
obj
)
}
/**
*更换上报方式发数据
*/
...
...
@@ -73,7 +78,7 @@ class Tracker {
/**
* 用户登录绑定唯一标识
*/
login
(
id
:
string
)
{
login
(
id
:
string
|
number
)
{
user
.
setLoginId
(
id
)
}
/**
...
...
src/modules/clickEvent.ts
View file @
59aff8a
...
...
@@ -2,20 +2,19 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:10:56
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
14 17:38:12
* @LastEditTime: 2023-12-
21 09:20:19
* @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEupo
*/
import
Events
from
'./events'
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
{
TAG_PROP_NAME
,
TAGS
}
from
'../utils/constants'
import
collect
from
'./collect'
class
clickEvent
{
class
clickEvent
extends
Events
{
_options
:
any
/**
* 获取元素所有信息数据
,
* 获取元素所有信息数据
* @param target 接收的数据
*/
getEleDetail
(
target
:
HTMLElement
)
{
...
...
@@ -24,7 +23,7 @@ class clickEvent {
return
utils
.
extend
(
prop
,
res
)
}
/**
* 获取
元素信息数据,
* 获取
dom元素信息数据
class_name:类名
element_type:标签名
element_content:文本内容
...
...
@@ -100,10 +99,15 @@ class clickEvent {
return
target
}
}
/**
* 手动触发上报数据
* @param target 目标元素
* @param customProps 自定义属性
* @param callback 回调函数
*/
activeTrigger
(
target
:
HTMLElement
,
customProps
:
any
,
callback
:
Function
)
{
console
.
log
(
target
,
111
)
let
tagName
=
target
.
tagName
.
toLowerCase
()
if
(
this
.
getTargetElement
(
target
))
{
if
(
this
.
getTargetElement
(
target
)
||
this
.
_options
.
open_all_click
)
{
return
false
}
this
.
start
({
event
:
null
,
target
,
tagName
,
customProps
,
callback
})
...
...
@@ -127,20 +131,17 @@ class clickEvent {
}
/**
* 初始化,并绑定监听事件
* params
* params
配置信息
*/
init
<
T
>
(
this
:
clickEvent
,
data
:
T
)
{
this
.
_options
=
data
if
(
this
.
_options
.
open_all_click
)
{
Events
.
addEvent
(
document
.
documentElement
,
'click'
,
(
e
:
MouseEvent
)
=>
{
console
.
log
(
this
.
getEleDetail
(
e
.
target
as
HTMLElement
))
this
.
addEvent
(
document
.
documentElement
,
'click'
,
(
e
:
MouseEvent
)
=>
{
let
_target
=
e
.
target
as
HTMLElement
this
.
start
({
event
:
e
,
target
:
_target
,
tagName
:
_target
.
tagName
.
toLowerCase
()
})
})
}
else
{
Events
.
addEvent
(
document
.
documentElement
,
'click'
,
(
e
?:
MouseEvent
)
=>
{
console
.
log
(
e
)
if
(
!
e
)
{
return
false
}
this
.
addEvent
(
document
.
documentElement
,
'click'
,
(
e
:
MouseEvent
)
=>
{
let
target
=
e
.
target
as
HTMLElement
let
_target
=
this
.
getTargetElement
(
target
)
if
(
_target
)
{
...
...
@@ -149,6 +150,10 @@ class clickEvent {
})
}
}
/**
* 开始收集数据
* params 配置信息
*/
start
({
event
,
target
,
tagName
,
customProps
,
callback
}:
ClickParmasDescriptor
)
{
if
(
typeof
callback
==
'function'
)
{
callback
.
call
(
null
)
...
...
src/modules/collect.ts
View file @
59aff8a
...
...
@@ -2,24 +2,23 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 17:05:34
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
15 16:36:17
* @LastEditTime: 2023-12-
21 09:28:02
* @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
,
ReportInfoDescriptor
}
from
'../types/collect'
import
utils
from
'../utils/helper'
import
user
from
'./user
'
import
{
EVENT_TYPE
}
from
'../utils/constants
'
class
Collect
{
/**
* 采集用户信息数据
* @param info 接收的数据
*/
getUserData
()
{
}
getUUID
:
string
user_login_id
?:
string
|
number
constructor
()
{
this
.
getUUID
=
utils
.
UUID
()
}
/**
* 采集默认信息数据
* @param info 接收的数据
*/
getDefaultData
():
DefaultInfoDescriptor
{
get
get
DefaultData
():
DefaultInfoDescriptor
{
return
{
$page_title
:
document
.
title
,
$page_url
:
location
.
href
,
...
...
@@ -31,33 +30,50 @@ class Collect {
* 判断需要监听哪些dom元素
*/
getClickEvent
<
T
extends
object
>
(
para
:
T
)
{
this
.
send
(
para
,
'clickEvent'
)
this
.
send
Tracker
(
para
,
EVENT_TYPE
[
0
]
as
EventType
)
}
/**
* 收集浏览事件数据
*/
getViewEvent
<
T
extends
object
>
(
para
:
T
)
{
this
.
send
(
para
,
'viewEvent'
)
this
.
send
Tracker
(
para
,
EVENT_TYPE
[
1
]
as
EventType
)
}
/**
* 收集停留事件数据
*/
getStayEvent
<
T
extends
object
>
(
para
:
T
)
{
this
.
send
(
para
,
'stayEvent'
)
this
.
sendTracker
(
para
,
EVENT_TYPE
[
2
]
as
EventType
)
}
/**
* 设置登录用户id
*/
getLoginId
(
id
:
string
|
number
)
{
this
.
user_login_id
=
id
}
/**
* 发送特殊的埋点数据
*/
sendCommonTracker
(
obj
:
object
,
name
:
string
)
{
const
reportInfo
=
{
...
obj
,
eventName
:
name
,
report_time
:
utils
.
dateFormat
(
Date
.
now
()),
anonymous_id
:
this
.
getUUID
}
console
.
log
(
reportInfo
)
}
/**
* 发送埋点数据
*/
send
<
T
>
(
para
:
T
,
type
:
EventType
)
{
send
Tracker
<
T
>
(
para
:
T
,
type
:
EventType
)
{
const
reportInfo
:
ReportInfoDescriptor
=
{
properties
:
Object
.
assign
(
this
.
getDefaultData
()
,
para
),
properties
:
Object
.
assign
(
this
.
getDefaultData
,
para
),
eventName
:
type
,
anonymous_id
:
'888'
,
send
_time
:
utils
.
dateFormat
(
Date
.
now
())
anonymous_id
:
this
.
getUUID
,
report
_time
:
utils
.
dateFormat
(
Date
.
now
())
}
if
(
user
.
user_login_id
)
{
reportInfo
.
user_login_id
=
user
.
user_login_id
if
(
this
.
user_login_id
)
{
reportInfo
.
user_login_id
=
this
.
user_login_id
}
console
.
log
(
reportInfo
)
}
...
...
src/modules/events.ts
View file @
59aff8a
...
...
@@ -2,13 +2,13 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 17:15:38
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
14 14:58:34
* @LastEditTime: 2023-12-
20 17:04:53
* @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
class
Events
{
export
default
class
Events
{
/**
* 给元素添加事件
,并注册事件
* 给元素添加事件
* @param element 接收的数据
*/
addEvent
(
element
:
HTMLElement
|
Window
,
type
:
string
,
handler
:
Function
)
{
...
...
@@ -34,4 +34,3 @@ class Events {
)
}
}
export
default
new
Events
()
src/modules/stayEvent.ts
View file @
59aff8a
...
...
@@ -2,24 +2,20 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:46:13
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
15 16:49:25
* @LastEditTime: 2023-12-
20 21:16:49
* @FilePath: \glsx-tracker-javascript\src\modules\stayEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
console
.
log
(
222
)
import
type
{
StayDescriptor
}
from
'../types/stay'
import
collect
from
'./collect'
import
Events
from
'./events'
class
StayEvent
{
class
StayEvent
extends
Events
{
static
instance
:
StayEvent
|
undefined
_options
:
any
start_time
:
number
=
Date
.
now
()
timeout_time
:
number
=
1000
timer
:
NodeJS
.
Timeout
|
undefined
interval_time
:
number
=
5000
constructor
()
{
console
.
log
(
1111111
)
}
/**
* 获取视口位置
*/
...
...
@@ -53,7 +49,7 @@ class StayEvent {
* params
*/
trigger
(
this
:
StayEvent
)
{
Event
s
.
addEvent
(
window
,
'scroll'
,
(
e
:
MouseEvent
)
=>
{
thi
s
.
addEvent
(
window
,
'scroll'
,
(
e
:
MouseEvent
)
=>
{
if
(
this
.
_options
.
collect_url
&&
!
this
.
_options
.
collect_url
())
{
return
false
}
...
...
@@ -84,14 +80,5 @@ class StayEvent {
this
.
_options
=
data
this
.
trigger
()
}
/**
* 单例模式
*/
static
getInstance
()
{
if
(
!
StayEvent
.
instance
)
{
StayEvent
.
instance
=
new
StayEvent
()
}
return
StayEvent
.
instance
}
}
export
default
new
StayEvent
()
src/modules/user.ts
View file @
59aff8a
...
...
@@ -2,39 +2,83 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-12-15 15:49:29
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
15 16:35:4
3
* @LastEditTime: 2023-12-
20 22:29:2
3
* @FilePath: \glsx-tracker-javascript\src\modules\attribute.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import
{
SDK_version
}
from
'../utils/constants'
import
{
DeviceInfoDescriptor
}
from
'../types/user'
import
{
SDK_version
,
SPECIAL_EVENT_TYPE
}
from
'../utils/constants'
import
{
DefaultInfoDescriptor
}
from
'../types/user'
import
collect
from
'./collect'
class
User
{
user_login_id
?:
string
|
number
userInfo
?:
object
constructor
()
{
collect
.
sendCommonTracker
(
{
...
this
.
getDefaultData
},
SPECIAL_EVENT_TYPE
[
2
]
)
}
/**
* 设置用户登录注册id
*/
user_login_id
?:
string
userInfo
?:
object
setLoginId
(
id
:
string
)
{
setLoginId
(
id
:
string
|
number
)
{
this
.
user_login_id
=
id
collect
.
getLoginId
(
id
)
collect
.
sendCommonTracker
(
{
user_login_id
:
id
},
SPECIAL_EVENT_TYPE
[
0
]
)
}
/**
* 设置用户信息
*/
setUser
(
obj
:
object
)
{
setUser
(
obj
:
object
)
{
this
.
userInfo
=
obj
collect
.
sendCommonTracker
(
{
...
obj
},
SPECIAL_EVENT_TYPE
[
0
]
)
}
/**
* 采集设备信息数据
* @param info 接收的数据
* 采集默认信息数据
* @return {
* $screen_width:屏幕宽度
$screen_height: 屏幕高度
$plateform: 平台系统
$appName: ,
$userAgent:,
$appVersion:
* }
*/
getDeviceData
():
Device
InfoDescriptor
{
get
getDefaultData
():
Default
InfoDescriptor
{
return
{
SDK_version
,
$screen_width
:
window
.
screen
.
width
,
$screen_height
:
window
.
screen
.
height
,
$plateform
:
navigator
.
platform
,
$appName
:
navigator
.
appName
,
$userAgent
:
navigator
.
userAgent
,
$appVersion
:
navigator
.
appVersion
}
}
/**
* 添加公共属性
*/
addCommonData
(
prop
:
object
)
{
collect
.
sendCommonTracker
(
{
...
this
.
getDefaultData
,
...
prop
},
SPECIAL_EVENT_TYPE
[
3
]
)
}
}
export
default
new
User
()
src/modules/viewEvent.ts
View file @
59aff8a
...
...
@@ -2,7 +2,7 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:45:53
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
14 17:36:55
* @LastEditTime: 2023-12-
21 09:25:12
* @FilePath: \glsx-tracker-javascript\src\modules\viewEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
...
...
@@ -10,9 +10,9 @@ import type { viewBase, routerOption } from '../types/view'
import
Events
from
'./events'
import
utils
from
'../utils/helper'
import
collect
from
'./collect'
class
ViewEvent
{
class
ViewEvent
extends
Events
{
baseParams
:
viewBase
last_time
:
string
last_time
:
string
routerModel
:
routerOption
|
undefined
=
'hash'
/**
* 初始化页面信息
...
...
@@ -25,6 +25,7 @@ class ViewEvent {
$page_referrer: 页面来源
*/
constructor
()
{
super
()
this
.
last_time
=
utils
.
dateFormat
(
new
Date
())
this
.
baseParams
=
{
$page_title
:
document
.
title
,
...
...
@@ -60,7 +61,7 @@ class ViewEvent {
*/
captureEvents
<
T
>
(
MouseEventList
:
string
[])
{
MouseEventList
.
forEach
(
event
=>
{
Event
s
.
addEvent
(
window
,
event
,
(
e
:
MouseEvent
)
=>
{
thi
s
.
addEvent
(
window
,
event
,
(
e
:
MouseEvent
)
=>
{
this
.
trigger
()
})
})
...
...
@@ -88,7 +89,7 @@ class ViewEvent {
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
.
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
)
...
...
src/types/collect.d.ts
View file @
59aff8a
...
...
@@ -15,6 +15,6 @@ export interface ReportInfoDescriptor {
properties
:
any
eventName
:
EventType
anonymous_id
:
string
send
_time
:
string
user_login_id
?:
string
report
_time
:
string
user_login_id
?:
string
|
number
}
src/types/main.ts
View file @
59aff8a
...
...
@@ -4,14 +4,14 @@ 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-
15 14:41:1
8
* @LastEditTime: 2023-12-
20 20:01:3
8
* @FilePath: \glsx-tracker-javascript\src\types\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
export
type
trackType
=
'beacon'
|
'image'
|
'ajax'
|
'fetch'
export
type
ClickOption
=
{
[
prop
:
string
]:
any
open_all_click
:
false
open_all_click
:
boolean
}
export
type
ScrollOption
=
{
[
prop
:
string
]:
any
...
...
src/types/user.d.ts
View file @
59aff8a
export
interface
De
vice
InfoDescriptor
{
export
interface
De
fault
InfoDescriptor
{
$plateform
:
string
$appName
:
string
$userAgent
:
string
$appVersion
:
string
[
prop
:
string
]:
string
$screen_width
:
number
$screen_height
:
number
[
prop
:
string
]:
any
}
src/utils/constants.ts
View file @
59aff8a
...
...
@@ -2,9 +2,22 @@
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-12-13 14:03:18
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-
15 13:37:11
* @LastEditTime: 2023-12-
21 09:49:02
* @FilePath: \glsx-tracker-javascript\src\utils\constants.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
export
const
TAG_PROP_NAME
=
'data-gl-tracker'
export
const
SDK_version
=
'v1.0.0'
export
const
SAME_TRACKER
=
'user_info_tracker'
export
const
TAGS
=
[
'a'
,
'input'
,
'textarea'
,
'button'
]
export
enum
EVENT_TYPE
{
'clickEvent'
,
'stayEvent'
,
'viewEvent'
}
export
enum
SPECIAL_EVENT_TYPE
{
'login'
,
'user_properties'
,
'common_properties'
,
'add_common_properties'
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment