n/programu/jinkesi · 2024-10-31
注册 登录
n/program · 2024-10-31 · 403 次浏览
u/jinkesi白银

分享PHP主机搭建Umami的统计API并更改到Hexo安知鱼主题关于页面

### 在本文中,我们将探讨如何使用PHP来搭建Umami API。Umami是一个简单、快速、隐私友好的替代Google Analytics的开源解决方案。通过构建自己的API,我们可以更灵活地访问和使用Umami收集的数据。

*>*感谢~~梦爱吃鱼提供的关于页魔改和~~张洪Heo提供的获取Token方法

## 1. 准备工作

  • - 已部署的Umami实例

  • - 已添加好Umami的网站

### 2. 首先进入~~~~Hoppscotch 获取token

</i>{
"username": "umami账户",
"password": "umami密码"
}<i>
</i><e>```</e>
https://你的Umami网站地址/api/auth/login
<UPL-IMAGE-PREVIEW url="[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730282995-591419-043616ee60f928f0d54785d4aeaa3676.png](/uploads/default/2024-10-30/1730282995-591419-043616ee60f928f0d54785d4aeaa3676.png)">[upl-image-preview url=[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730282995-591419-043616ee60f928f0d54785d4aeaa3676.png](/uploads/default/2024-10-30/1730282995-591419-043616ee60f928f0d54785d4aeaa3676.png)]</UPL-IMAGE-PREVIEW>



****~~成功后会返回Token信息<e>**</e>**


<UPL-IMAGE-PREVIEW url="[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283016-355339-28e690af-2e09-4c82-8da2-88e0b2f45bd6.png](/uploads/default/2024-10-30/1730283016-355339-28e690af-2e09-4c82-8da2-88e0b2f45bd6.png)">[upl-image-preview url=[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283016-355339-28e690af-2e09-4c82-8da2-88e0b2f45bd6.png](/uploads/default/2024-10-30/1730283016-355339-28e690af-2e09-4c82-8da2-88e0b2f45bd6.png)]</UPL-IMAGE-PREVIEW>


<LIST start="2" type="decimal">
- ~~2.~~ 然后访问 <C>~~`</s>张洪Heo<e>`</e></C> <URL url="https://github.com/zhheo/HeoPVBridge/blob/main/umami/info.php">[~~Github修改的API下载<e>](https://github.com/zhheo/HeoPVBridge/blob/main/umami/info.php)</e></URL>,按照注释更改里面的<C>~~`</s>地址<e>`</e></C>、<C>`</s>token<e>`</e></C> 和<C>`</s>网站id<e>`</e></C>。然后部署到网站的php项目中即可。</LIST>

``` PHP</s><i>
</i>// 配置 Umami API 的凭据
$apiBaseUrl = 'https://xxxx';  // 你的网站ID
$token = '你的tocken';         // 上面网站获取的Tokeen         
$websiteId = 'Umami你的网站id';  // Umami你的网站id
$cacheFile = 'umami_cache.json';
$cacheTime = 600; // 缓存时间为10分钟(600秒)<i>
</i><e>```</e>

<LIST start="3" type="decimal">
- 3.~~ 然后我们把刚刚下载修改好的文件修改后上传到serv00里面</LIST>

<UPL-IMAGE-PREVIEW url="[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283028-304151-709a46f8-945f-4270-9408-18a7d11398e8.png](/uploads/default/2024-10-30/1730283028-304151-709a46f8-945f-4270-9408-18a7d11398e8.png)">[upl-image-preview url=[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283028-304151-709a46f8-945f-4270-9408-18a7d11398e8.png](/uploads/default/2024-10-30/1730283028-304151-709a46f8-945f-4270-9408-18a7d11398e8.png)]</UPL-IMAGE-PREVIEW>
<UPL-IMAGE-PREVIEW url="[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283035-915959-d570001d-3026-41c7-80e1-bd9a808d1c7c.png](/uploads/default/2024-10-30/1730283035-915959-d570001d-3026-41c7-80e1-bd9a808d1c7c.png)">[upl-image-preview url=[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283035-915959-d570001d-3026-41c7-80e1-bd9a808d1c7c.png](/uploads/default/2024-10-30/1730283035-915959-d570001d-3026-41c7-80e1-bd9a808d1c7c.png)]</UPL-IMAGE-PREVIEW>
<UPL-IMAGE-PREVIEW url="[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283072-39874-987fc54f-5241-435b-b7f2-722ea4fe89ee.png](/uploads/default/2024-10-30/1730283072-39874-987fc54f-5241-435b-b7f2-722ea4fe89ee.png)">[upl-image-preview url=[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283072-39874-987fc54f-5241-435b-b7f2-722ea4fe89ee.png](/uploads/default/2024-10-30/1730283072-39874-987fc54f-5241-435b-b7f2-722ea4fe89ee.png)]</UPL-IMAGE-PREVIEW>


<LIST start="4" type="decimal">
- ~~4.~~ 接下来我们访问你的域名/你的文件名就可以了。</LIST>

**~~**~~比如我的是<e>**</e>** : <URL url="https://umami.200038.xyz/umapi.php">https://umami.200038.xyz/umapi.php</URL> 打开后如下图所示


<UPL-IMAGE-PREVIEW url="[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283104-110363-f5babdf0-dbf9-49da-81af-5325fdf52c18.png](/uploads/default/2024-10-30/1730283104-110363-f5babdf0-dbf9-49da-81af-5325fdf52c18.png)">[upl-image-preview url=[https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283104-110363-f5babdf0-dbf9-49da-81af-5325fdf52c18.png](/uploads/default/2024-10-30/1730283104-110363-f5babdf0-dbf9-49da-81af-5325fdf52c18.png)]</UPL-IMAGE-PREVIEW>


<LIST start="5" type="decimal">
- ~~5.~~ 如何更改 <C>~~`</s>UmamiAPI<e>`</e></C> 到安知鱼主题关于页面参考 <URL url="https://blog.bsgun.cn/posts/609bcdb9/">[~~梦爱吃鱼<e>](https://blog.bsgun.cn/posts/609bcdb9/)</e></URL></LIST>

~~####~~ 1. 在 <C>~~`</s>themes/anzhiyu/layout/includes/head.pug<e>`</e></C> 添加


``` PUG~~
//- Umami
if theme.Umami
if theme.Umami.umami_url
script(async defer src=`${theme.Umami.umami_url_js}` data-website-id=`${theme.Umami.umami_id}` data-host-url=`${theme.Umami.umami_url}`)
else
script(async defer src=`${theme.Umami.umami_url_js}` data-website-id=`${theme.Umami.umami_id}`)
``` 


#### 2. 然后修改 `themes/anzhiyu/source/css/_page/about.styl`

``` STYL
大致在1255行
- if (hexo-config('LA.enable')) {
+ if (hexo-config('LA.enable') || hexo-config('Umami.enable')) {<i>
</i><e>```</e></CODE>

<H4><s>#### </s>3. 接着修改 <C><s>`</s>themes/anzhiyu/layout/includes/page/about.pug<e>`</e></C></H4>

<CODE lang="PUG"><s>``` PUG</s><i>
</i>//-  应该是91行
       .author-content
-        if theme.LA.enable
+        if theme.LA.enable || theme.Umami.enable
           - let cover = item.statistic.cover
           .about-statistic.author-content-item(style=`background: url(${cover}) top / cover no-repeat;`)
             .card-content
               .author-content-item-tips 数据
               span.author-content-item-title 访问统计
               #statistic
-              .post-tips
-                | 统计信息来自
-                a(href='https://invite.51.la/1NzKqTeb?target=V6', target='_blank', rel='noopener nofollow') 51la网站统计
+              if theme.LA.enable
+                .post-tips
+                  | 统计信息来自
+                  a(href='https://www.51.la/', target='_blank', rel='noopener nofollow') 51LA统计
+              else if theme.Umami.enable
+                .post-tips
+                  | 统计信息来自
+                  a(href='https://um.ruom.top', target='_blank', rel='noopener nofollow') Umami统计
               .banner-button-group
                 - let link = item.statistic.link
                 - let text = item.statistic.text<i>
</i><e>```</e></CODE>

<H4><s>#### </s>4. 继续修改 直接搜 <C><s>`</s>- const ck = theme.LA.ck<e>`</e></C> 把下面的全部替换</H4>

<CODE lang="PUG"><s>``` PUG</s><i>
</i>    //- Umami 统计 和 51LA 统计
    if theme.Umami &amp;&amp; theme.Umami.enable
      script(defer).
        (function() {
          const umamiApiUrl = "#{url_for(theme.Umami.umami_api)}";
          fetch(umamiApiUrl)
            .then(res =&gt; res.json())
            .then(data =&gt; {
              let title = {
                "today_uv": "今日人数",
                "today_pv": "今日访问",
                "yesterday_uv": "昨日人数",
                "yesterday_pv": "昨日访问",
                "last_month_pv": "本月访问",
                "last_year_pv": "本年访问"
              };
              let s = document.getElementById("statistic");
              for (let key in data) {
                if (data.hasOwnProperty(key) &amp;&amp; title[key]) {
                  s.innerHTML += `&lt;div&gt;&lt;span&gt;${title[key]}&lt;/span&gt;&lt;span id="${key}"&gt;${data[key]}&lt;/span&gt;&lt;/div&gt;`;
                }
              }
              initCountUp(data, title);
            })
            .catch(error =&gt; console.error('Error:', error));
        })();
    else
      script(defer).
        function initAboutPage() {
          fetch("https://v6-widget.51.la/v6/#{ck}/quote.js")
            .then(res =&gt; res.text())
            .then(data =&gt; {
              let title = ["最近活跃", "今日人数", "今日访问", "昨日人数", "昨日访问", "本月访问", "总访问量"];
              let num = data.match(/(&lt;\/span&gt;&lt;span&gt;).*?(\/span&gt;&lt;\/p&gt;)/g);

              num = num.map(el =&gt; {
                let val = el.replace(/(&lt;\/span&gt;&lt;span&gt;)/g, "");
                let str = val.replace(/(&lt;\/span&gt;&lt;\/p&gt;)/g, "");
                return str;
              });

              let statisticEl = document.getElementById("statistic");

              // 自定义不显示哪个或者显示哪个,如下为不显示 最近活跃访客 和 总访问量
              let statistic = [];
              for (let i = 0; i &lt; num.length; i++) {
                if (!statisticEl) return;
                if (i == 0) continue;
                statisticEl.innerHTML +=
                  "&lt;div&gt;&lt;span&gt;" + title[i] + "&lt;/span&gt;&lt;span id=" + title[i] + "&gt;" + num[i] + "&lt;/span&gt;&lt;/div&gt;";
                queueMicrotask(() =&gt; {
                  statistic.push(
                    new CountUp(title[i], 0, num[i], 0, 2, {
                      useEasing: true,
                      useGrouping: true,
                      separator: ",",
                      decimal: ".",
                      prefix: "",
                      suffix: "",
                    })
                  );
                });
              }

              let statisticElement = document.querySelector(".about-statistic.author-content-item");
              function statisticUP() {
                if (!statisticElement) return;

                const callback = (entries, observer) =&gt; {
                  entries.forEach(entry =&gt; {
                    if (entry.isIntersecting) {
                      for (let i = 0; i &lt; num.length; i++) {
                        if (i == 0) continue;
                        queueMicrotask(() =&gt; {
                          statistic[i - 1].start();
                        });
                      }
                      observer.disconnect(); // 停止观察元素,因为不再需要触发此回调
                    }
                  });
                };

                const options = {
                  root: null,
                  rootMargin: "0px",
                  threshold: 0
                };
                const observer = new IntersectionObserver(callback, options);
                observer.observe(statisticElement);
              }

              statisticUP();
              initCountUp({}, {});
            });

          initAnimation();
        }
        if (typeof gsap === "object") {
          initAboutPage()
        } else {
          getScript("!{url_for(theme.asset.gsap_js)}").then(initAboutPage);
        }

    //- 初始化 countup.js
    script(defer).
      function initCountUp(data, title) {
        const elements = [];

        for (let key in data) {
          if (data.hasOwnProperty(key) &amp;&amp; title[key]) {
            const element = document.getElementById(key);
            if (element) {
              elements.push({ id: key, value: data[key], element: element });
            }
          }
        }

        const selfInfoContentYearElement = document.getElementById("selfInfo-content-year");
        if (selfInfoContentYearElement) {
          elements.push({ id: "selfInfo-content-year", value: #{selfInfoContentYear}, element: selfInfoContentYearElement });
        }

        const observer = new IntersectionObserver((entries) =&gt; {
          entries.forEach(entry =&gt; {
            if (entry.isIntersecting) {
              const target = elements.find(el =&gt; el.element === entry.target);
              if (target) {
                const countUp = new CountUp(target.id, 0, target.value, 0, 2, {
                  useEasing: true,
                  useGrouping: target.id === "selfInfo-content-year" ? false : true,
                  separator: ",",
                  decimal: ".",
                  prefix: "",
                  suffix: "",
                });
                countUp.start();
                observer.unobserve(entry.target);
              }
            }
          });
        }, { threshold: 0 });

        elements.forEach(el =&gt; observer.observe(el.element));
      }

    //- 独立鼠标跟随动画
    script(defer).
      function initAnimation() {
        var pursuitInterval = null;
        pursuitInterval = setInterval(function () {
          const show = document.querySelector("span[data-show]");
          const next = show.nextElementSibling || document.querySelector(".first-tips");
          const up = document.querySelector("span[data-up]");

          if (up) {
            up.removeAttribute("data-up");
          }

          show.removeAttribute("data-show");
          show.setAttribute("data-up", "");

          next.setAttribute("data-show", "");
        }, 2000);

        document.addEventListener("pjax:send", function () {
          pursuitInterval &amp;&amp; clearInterval(pursuitInterval);
        });

        var helloAboutEl = document.querySelector(".hello-about");
        helloAboutEl.addEventListener("mousemove", evt =&gt; {
          const mouseX = evt.offsetX;
          const mouseY = evt.offsetY;
          gsap.set(".cursor", {
            x: mouseX,
            y: mouseY,
          });

          gsap.to(".shape", {
            x: mouseX,
            y: mouseY,
            stagger: -0.1,
          });
        });
      }
      if (typeof gsap === "object") {
        initAnimation()
      } else {
        getScript("!{url_for(theme.asset.gsap_js)}").then(initAnimation);
      }<i>
</i><e>```</e></CODE>

<H3><s>### </s>5. 最后在主题的 <C><s>`</s>config.yml<e>`</e></C> 配置项内添加</H3>

<CODE lang="YML"><s>``` YML</s><i>
</i># Umami
Umami:
  enable: true # 开关
  umami_url_js: https://um.200038.xyz/script.js # 填写 umami js地址 可以使用第三方CDN加速但需要配置下面的 umami_url
  umami_id: d4662f99-6e33-4665-9ae0-db72eda07b0b # 填写 umami 统计 ID
  umami_api: https://umami.200038.xyz/umapi.php # 填写 umami API 地址
  umami_url:   #填写 umami 服务器地址 使用 CDN 加速 Umami 静态资源后需配置此项  <i>
</i><e>```</e></CODE>

<LIST start="6" type="decimal"><LI><s>6. </s>不想更改也可以把 Umami 添加的网站引入到主题 <C><s>`</s>head<e>`</e></C> 或 <C><s>`</s>bottom<e>`</e></C>处 我一般添加到头部。我也是初学者哈哈哈</LI></LIST>

<QUOTE><i>&gt;</i><p>添加到主题更改我们只能从<C><s>`</s>Umami<e>`</e></C>网站中看到访问记录</p> </QUOTE>

<p><UPL-IMAGE-PREVIEW url="https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283133-887272-2.png">[upl-image-preview url=https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283133-887272-2.png]</UPL-IMAGE-PREVIEW></p>

<p><UPL-IMAGE-PREVIEW url="https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283143-325747-de34e202-cc5c-4166-b926-7f4eeac85c99.png">[upl-image-preview url=https://6waed6lo6phn6.pages.dev/uploads/default/2024-10-30/1730283143-325747-de34e202-cc5c-4166-b926-7f4eeac85c99.png]</UPL-IMAGE-PREVIEW></p>

</r>
0
0
登录后参与讨论。
海外原生IP VPS,稳定访问全球网络

还在为IP不纯、平台风控、频繁验证而烦恼? 丽萨主机海外原生IP VPS,双ISP住宅IP,覆盖美国、香港、日本、新加坡、台湾、德国、越南、英国等热门地区,适合跨境电商、社媒运营、AI工具、流媒体及海外业务使用。

lisahost.com 了解更多

0 条回复

排序

还没有回复,来说第一句。