# すべてのページ「ロード」でカスタム統計スクリプトをトリガーするにはどうすればよいですか？

**URL:** <https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026>\
**Category:** Development\
**Created:** [2023 年 8 月 4 日午後 9:09 UTC](https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026 "2023-08-04T21:09:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)\
**Post date:** [2023 年 8 月 4 日午後 9:36 UTC](https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026/2 "2023-08-04T21:36:22Z")

</div>

[plugin API](https://github.com/discourse/discourse/blob/85e2f673679bd88b23f3f8908241a03470eb617b/app/assets/javascripts/discourse/app/lib/plugin-api.js#L722) の `onPageChange` を使用するのが良いでしょう。

テーマ内では、`javascripts/discourse/api-initializers/your-initializer.js` に以下のようなものを追加すると良いでしょう。

```js
import { withPluginApi } from "discourse/lib/plugin-api";

export default {
  name: "custom-script-name",

  initialize() {
    withPluginApi("0.8.36", (api) => {
      api.onPageChange(() => {
        // ここにページ変更時に実行したいスクリプトを記述します
      });
    });
  },
};

```

---

_[View the full topic](https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026)._
