Forráskód Böngészése

feat: 新增页面信息、平台信息

zhangyuhan 2 éve
szülő
commit
995a0c2adc
2 módosított fájl, 95 hozzáadás és 16 törlés
  1. 45 15
      src/easy-use-track.js
  2. 50 1
      src/module/utils.js

+ 45 - 15
src/easy-use-track.js

@@ -1,5 +1,5 @@
 import Track from './core/index'
-import {loadJS} from './module/utils'
+import {getPlatform, loadJS} from './module/utils'
 
 /**
  * 更简易地注入荟聚埋点统计
@@ -52,8 +52,7 @@ class EasyUse {
       jyEventTrack.$subscribe.$on('CLICK-TRACK', function (transformedData, pageInfo) {
         if (_this.checkHasClabTracker()) {
           try {
-            const customInfo = window.jy_tarck_info || {}
-            clab_tracker.track('c_jyclick', {
+            clab_tracker.track('c_jyclick', Object.assign({
               c_breakername: transformedData.data.content.substr(0, 15),
               c_pagename: pageInfo.href.title,
               c_platform: pageInfo.platform,
@@ -64,7 +63,7 @@ class EasyUse {
               c_desc: '',
               c_title: pageInfo.href.title,
               date: new Date(transformedData.data.event_time)
-            }, _this.getNowPageInfo('click'), customInfo)
+            }, _this.getNowPageInfo('click')))
           } catch (e) {
             console.warn('!! Check Track JS Error 001 !!')
           }
@@ -94,19 +93,31 @@ class EasyUse {
    */
   addPageChangeListener() {
     window.addEventListener("popstate",  (e) => {
-      try {
-        const params = this.getNowPageInfo('popstate')
-        clab_tracker.ready(function () {
-          this.push({"pageType":"web"});
-          this.track("open_page", params);
-          clab_tracker.setEnableAutoTrackExitPage(false)
-        })
-      } catch (e) {
-        console.warn('!! Check Track JS Error 003 !!')
-      }
+      this.upPageInfo()
     }, false)
   }
 
+  /**
+   * 上报页面信息
+   */
+  upPageInfo () {
+    const params = this.getNowPageInfo('popstate')
+    try {
+      clab_tracker.ready(function () {
+        this.push({"pageType":"web"});
+        this.track("open_page", params);
+        clab_tracker.setEnableAutoTrackExitPage(false)
+      })
+    } catch (e) {
+      console.warn('!! Check Track JS Error 003 !!')
+    }
+    try {
+      clab_tracker.track('c_jy_open_page', params)
+    } catch (e) {
+      console.warn('!! Check Track JS Error 004 !!')
+    }
+  }
+
   /**
    * 获取当前页面参数
    * @param type - 事件类型
@@ -119,6 +130,24 @@ class EasyUse {
       this.params.identityType = "c_uid"
       this.params.identityValue = this.getCookie('ud_safe')
     }
+    // 页面信息
+    this.params.c_title = document.title
+    this.params.c_platform = getPlatform()
+    const pageInfo = this.tryFindInfo()
+    if (pageInfo) {
+      if (pageInfo['页面ID']) {
+        this.params.c_pageid = pageInfo['页面ID']
+      }
+      if (pageInfo['页面名称']) {
+        this.params.c_pagename = pageInfo['页面名称']
+      }
+      if (pageInfo['产品名称']) {
+        this.params.c_productname = pageInfo['产品名称']
+      }
+      if (pageInfo['页面功能简述']) {
+        this.params.c_desc = pageInfo['页面功能简述']
+      }
+    }
     return Object.assign({}, this.params, customInfo)
   }
 
@@ -128,7 +157,7 @@ class EasyUse {
    * @returns {{}}
    */
   tryFindInfo (url = location.href) {
-    let result = {}
+    let result = false
     for (let i =0;i<this.json.length;i++) {
       const item = this.json[i]
       const matchLink = decodeURIComponent(item['页面URL'])
@@ -153,6 +182,7 @@ class EasyUse {
     this.getJSON(this.config.dataSource, (data) => {
       if (Array.isArray(data)) {
         this.json = [].concat(data)
+        this.upPageInfo()
       }
     })
   }

+ 50 - 1
src/module/utils.js

@@ -119,7 +119,9 @@ function getNodeSelector(el) {
  * @returns {number} - 1-pc端 2-微信端 3-APP端 4-小程序 9-其他
  * */
 export function getPlatform() {
-  if (isApp()) {
+  if (getIsH5HostName()) {
+    return 'H5'
+  } else if (isApp()) {
     return 'APP'
   } else if (isWeChatMP()) {
     return 'WeChatMP'
@@ -373,3 +375,50 @@ export function loadJS (url, success) {
   }
   document.getElementsByTagName('head')[0].appendChild(domScript);
 }
+
+// 在安卓或者ios中
+export const androidOrIOS = function () {
+  const u = ua.toLowerCase()
+  let agent = ''
+  if (/iphone|ipod|ipad|ios/.test(u)) {
+    agent = 'ios'
+  } else {
+    agent = 'android'
+  }
+  return agent
+}
+
+/**
+ * 用于判断是否在APP容器内
+ * @returns {boolean}
+ */
+export const getIsInTheAppContainer = function () {
+  const u = navigator.userAgent.toLowerCase()
+  // 判断是否在app环境下
+  let inApp = false
+  // app调试ua
+  if (u.includes('jianyuapp')) {
+    inApp = true
+    return inApp
+  }
+  if (window.JyObj && window.JyObj.mock) {
+    return inApp
+  }
+  try {
+    if (androidOrIOS() === 'ios') {
+      const iniOSApp = typeof window.webkit.messageHandlers.skipAppointTab.postMessage === 'function'
+      inApp = iniOSApp
+    } else {
+      const inAndroidApp = typeof window.JyObj !== 'undefined'
+      inApp = inAndroidApp
+    }
+  } catch (e) {
+    console.warn(e)
+    inApp = false
+  }
+  return inApp
+}
+
+export const getIsH5HostName = () => {
+  return location.hostname.toLowerCase().includes('h5')
+}