viewRecordMixin.js 1.12 KB
/**
 * 设备查看记录埋点 mixin
 * 用于详情页:进入时 StartViewRecord,离开时 EndViewRecord
 * 使用:在详情页 mixins: [viewRecordMixin],数据加载成功后调用 this.startViewTracking(reId, type, contentName, deviceName)
 * 记录类型、设备名称、查看内容 三字段便于定位
 */
export default {
	data() {
		return {
			viewRecordId: null
		}
	},
	methods: {
		async startViewTracking(reId, type, contentName, deviceName) {
			if (!reId || !type) return
			try {
				const data = { reId: String(reId), type }
				if (contentName) data.contentName = String(contentName)
				if (deviceName) data.deviceName = String(deviceName)
				const res = await this.API.startViewRecord(data)
				if (res && res.data) {
					this.viewRecordId = res.data
				}
			} catch (e) {
				console.warn('StartViewRecord failed', e)
			}
		},
		async endViewTracking() {
			if (!this.viewRecordId) return
			try {
				await this.API.endViewRecord({ recordId: this.viewRecordId })
			} catch (e) {
				console.warn('EndViewRecord failed', e)
			}
			this.viewRecordId = null
		}
	},
	onUnload() {
		this.endViewTracking()
	}
}