# 统计用户（非工作人员、非爬虫）的页面浏览量

**URL:** <https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071>\
**Category:** Data & reporting\
**Created:** [2020年七月7日 19:24 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071 "2020-07-07T19:24:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrjn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrjn/32/121540_2.png) [@mrjn](https://meta.discourse.org/u/mrjn)\
**Post date:** [2020年七月7日 19:24 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/1 "2020-07-07T19:24:14Z")

</div>

大家好，

我在报告中没看到这些数据。我正在使用 [Data Explorer](https://meta.discourse.org/t/32566?silent=true) 插件，请问什么样的查询可以获取 **仅我们用户每周的页面浏览量** ，同时排除爬虫活动和论坛工作人员，但包含匿名页面浏览？

- 类似这样的查询可行吗？
- 它会考虑匿名浏览吗？
- 另外，有没有办法将其转换为报告？

```plaintext
-- [params]
-- string :date = '2020-01-01'

WITH t AS (
    SELECT date(:date) AS START
),
f AS (
    SELECT id as user_id
    FROM users u
    WHERE u.moderator = 'True' or u.admin = 'True'
)

    SELECT
    date_part('year', viewed_at) as year,
    date_part('week', viewed_at) as week,
    COUNT(topic_id) AS topics_viewed
    FROM topic_views, t
    WHERE viewed_at > t.START
        AND user_id not in (select f.user_id from f)
    GROUP BY year, week
    ORDER BY year, week

```

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2020年七月7日 19:47 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/2 "2020-07-07T19:47:40Z")

</div>

该查询看起来应该能正常工作。

> [@mrjn](#):
>
> 它会考虑匿名浏览吗？

是的，按当前写法，它会统计匿名浏览。对于匿名主题浏览，`user_id` 将为 `NULL`，而 `ip_address` 将不为 `NULL`。您可以通过在查询中添加以下条件来排除匿名浏览：

```sql
AND user_id IS NOT NULL

```

> [@mrjn](#):
>
> 有没有办法将其转换为报告？

这可以通过插件实现。Discourse Solved 插件会在管理仪表板中添加一份报告。该插件中可能有一些代码可作为如何添加报告的示例：[discourse-solved/plugin.rb at main · discourse/discourse-solved · GitHub](https://github.com/discourse/discourse-solved/blob/master/plugin.rb#L320%E3%80%82)

---

<div class="post-metadata">

**Author:** ![mrjn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrjn/32/121540_2.png) [@mrjn](https://meta.discourse.org/u/mrjn)\
**Post date:** [2020年七月7日 20:02 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/3 "2020-07-07T20:02:59Z")

</div>

那么，像这样？那么，爬虫的 IP 地址会是 null 吗？

我想包含：

- 匿名用户
- 已登录用户

我想排除：

- 爬虫
- 工作人员用户

```plaintext
-- [参数]
-- string :date = '2020-01-01'
-- boolean :staff = false

WITH t AS (
    SELECT date(:date) AS START
),
f AS (
    SELECT id as user_id
    FROM users u
    WHERE u.moderator = 'True' or u.admin = 'True'
)

    SELECT
    user_id,
-- date_part('year', viewed_at) as year,
-- date_part('week', viewed_at) as week,
    COUNT(topic_id) AS topics_viewed
    FROM topic_views, t
    WHERE viewed_at > t.START
    AND ((ip_address is not null AND user_id is NULL) OR
        user_id not in (select f.user_id from f))
    GROUP BY user_id
    ORDER BY topics_viewed DESC
    -- GROUP BY year, week
    -- ORDER BY year, week

```

---

<div class="post-metadata">

**Author:** ![mrjn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrjn/32/121540_2.png) [@mrjn](https://meta.discourse.org/u/mrjn)\
**Post date:** [2020年七月7日 21:04 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/4 "2020-07-07T21:04:08Z")

</div>

上述内容有误。与 Discourse 报告中的汇总页面浏览量相比，它对匿名用户进行了_低估_。

看来我可能还需要考虑其他一些表？

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2020年七月7日 21:23 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/5 "2020-07-07T21:23:06Z")

</div>

页面浏览量报告查询 `application_requests` 表。然而，该表无法提供用于过滤员工浏览的数据。其返回的是每种类型请求按日统计的总和。`req_type` 以整数形式返回，这些整数根据以下规则映射到相应的请求类型：

```plaintext
"http_total" => 0,
"http_2xx" => 1,
"http_background" => 2,
"http_3xx" => 3,
"http_4xx" => 4,
"http_5xx" => 5,
"page_view_crawler" => 6,
"page_view_logged_in" => 7,
"page_view_anon" => 8,
"page_view_logged_in_mobile" => 9,
"page_view_anon_mobile" => 10

```

您的查询所使用的 `topic_views` 表每个用户或 IP 地址针对每个主题每天最多返回一条记录。该数据无法用于跟踪页面浏览量，因为单次主题浏览可能对应多个应用请求。

---

<div class="post-metadata">

**Author:** ![mrjn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrjn/32/121540_2.png) [@mrjn](https://meta.discourse.org/u/mrjn)\
**Post date:** [2020年七月7日 21:26 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/6 "2020-07-07T21:26:22Z")

</div>

明白了。因此，要获得准确的页面浏览量（PV）数据，排除内部员工用户是不可行的。

我冒昧地猜测一下，是否可以排除特定类别（我猜不行）？我们有一些仅供团队内部使用的私密类别，不希望这些数据污染我们的用户 PV 统计。

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2020年七月7日 21:34 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/7 "2020-07-07T21:34:52Z")

</div>

> [@mrjn](#):
>
> 因此，如果不排除工作人员用户，就无法获得准确的页面浏览量（PV）数据。

我未发现有任何方法可以从 `application_requests` 表提供的数据中排除工作人员。同样，也无法按类别过滤该表的查询结果。如果您旨在获取关于站点使用情况的数据，您在原始查询中使用的 `topic_views` 表将提供有关站点使用情况的准确数据，但该数据与您“页面浏览量”报告中看到的数据并不一致。

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2020年八月6日 21:45 UTC](https://meta.discourse.org/t/counting-pageviews-for-users-non-staff-non-crawler/157071/8 "2020-08-06T21:45:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
