49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import useState from '~/hooks/useState'
|
|
import { MessageType, TrackActionType } from '~/typs'
|
|
import { show_message,send_data} from '~/hooks/utils'
|
|
|
|
export const track_action = async(e: any, src?: {ref: string,text?: string}, act = 'click') => {
|
|
const url = useState().CONFURLS.value.tracking || ''
|
|
if (url.length == 0) {
|
|
return
|
|
}
|
|
let href=''
|
|
let text=''
|
|
if (src && src.ref && src.ref.length > 0 ) {
|
|
href=src.ref
|
|
text=src.text ? src.text : src.ref
|
|
} else if (e && e.target) {
|
|
switch(e.target.tagName) {
|
|
case 'IMG':
|
|
const imgTarget = e.target.parentNode.tagName === 'A' ? e.target.parentNode : null
|
|
if (imgTarget && imgTarget.tagName === 'A') {
|
|
href = imgTarget.href ? imgTarget.href : ''
|
|
text = e.target.alt ? e.target.alt : e.target.src.slice(-1)[0]
|
|
}
|
|
break
|
|
case 'A':
|
|
const linkTarget = e.target
|
|
if (linkTarget && linkTarget.tagName === 'A') {
|
|
href = linkTarget.href ? linkTarget.href : ''
|
|
text = linkTarget.innerHTML ? linkTarget.innerHTML : ''
|
|
}
|
|
break
|
|
}
|
|
}
|
|
const trackAction: TrackActionType = {
|
|
when: Date.now().toString(),
|
|
where: `${useState().APPNAME.value}>${text}: ${href}`,
|
|
what: act,
|
|
context: navigator.userAgent,
|
|
data: useState().userID.value,
|
|
}
|
|
const [_res,err] = await send_data(url, trackAction, true, true)
|
|
if (err && err.length > 0 ) {
|
|
show_message(MessageType.Error, `Error: ${err}`,2000)
|
|
}
|
|
}
|
|
|
|
export default {
|
|
track_action,
|
|
}
|