Skip to content

外链跳转且埋点

html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <title th:text="|${article.title}_{{ conf.title }}|">
      {{ conf.title }}
    </title>
    <link rel="icon" href="/assets/images/favicon.png" />

    <meta name="sid" th:content="${config.appId}" />
    <meta name="catalogsId" th:content="${config.mainColumnId}" />
    <meta name="businessId" th:content="${config.businessId}" />
    <meta name="businessType" th:content="${config.businessType}" />
    <meta name="title" th:content="${article.title}" />
  </head>

  <body>
    <hlx-template
      component-type="news"
      component-query-option="{'newsId':''}"
      component-variable="article"
    ></hlx-template>

    <hlx-template
      component-type="comConfig"
      component-query-option=""
      component-variable="config"
    ></hlx-template>

    <script th:inline="javascript">

      var hlxApiIsSend = {
        currency:false,
        save:false,
      };

      var currency_path = '/foreground-gateway/statistics/currency';
      var save_path = '/ugc/userSurfInfo/save';

      (function () {
        var originalOpen = XMLHttpRequest.prototype.open;
        var originalSend = XMLHttpRequest.prototype.send;

        XMLHttpRequest.prototype.open = function (method, url) {
          this.__method = method;
          this.__url = url;
          return originalOpen.apply(this, arguments);
        };

        XMLHttpRequest.prototype.send = function (body) {
          // IE10 兼容:使用 indexOf 替代 includes
          if (this.__url && this.__url.indexOf(currency_path) !== -1) {
            console.log("XHR 请求了 currency 接口", this.__method, this.__url);
            hlxApiIsSend.currency = true;
          }
          if (this.__url && this.__url.indexOf(save_path) !== -1) {
            console.log("XHR 请求了 save 接口", this.__method, this.__url);
            hlxApiIsSend.save = true;
          }

          this.addEventListener("readystatechange", function () {
            if(hlxApiIsSend.currency && hlxApiIsSend.save){
              goToOutLink();
            }
          });

          return originalSend.call(this, body);
        };

      })();


      function goToOutLink() {
        var article = [[${article}]];
          console.log("article:", article)
          console.log("pcUrl :", article.pcUrl)
          console.log("extend_field_is_out_link :", article.formMap.extend_field_is_out_link)
          console.log("extend_field_out_link :", article.formMap.extend_field_out_link)

          if(article && article.formMap && article.formMap.extend_field_out_link){
            debugger;
            window.location.href = article.formMap.extend_field_out_link
          }
      }
    </script>

    {% if conf.isHlx %}
    <script type="text/javascript" src="/public/hlx-buried-point.js"></script>
    {% endif %}
  </body>
</html>