# How can I trigger my custom statistics script on every page "load"?

**URL:** https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026
**Category:** Development
**Created:** [August 4, 2023, 9:09pm 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:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nodomain](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nodomain/32/303402_2.png) [@nodomain](https://meta.discourse.org/u/nodomain)
#### Post date: [August 4, 2023, 9:09pm UTC](https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026/1 "2023-08-04T21:09:21Z")

</div>

Knowing that Discourse is a SPA and having looked at some docs around this topic I am still not sure how to achieve this:

- we include a custom js library in the footer that interfaces with our statistics solution
- we want to enrich the stats by triggering custom events on every “page load”, e.g whenever the user navigates to a “new” page

Pseudo code:

```javascript
window.statistic = window.statistic || [];
window.statistic.push({
    action: "page.ready",
    data: {
        page: {
            path: "/c/new-to-this-forum-get-started-here/20/l/new",
            country: "WW",
            language: "en",
}, user: {
            country: "DE",
            loginStatus: "logged_in"
        }
} });

```

How and where in my custom theme do I need to place the code to achieve this?

Thanks 🙂

---

<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: [August 4, 2023, 9:36pm 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>

You’ll likely want to use `onPageChange` from our [plugin API](https://github.com/discourse/discourse/blob/85e2f673679bd88b23f3f8908241a03470eb617b/app/assets/javascripts/discourse/app/lib/plugin-api.js#L722).

In a theme, you’d want to add something like this to `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(() => {
        // script you want to fire on page change here
      });
    });
  },
};

```

---

<div class="post-metadata">

### Author: ![nodomain](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nodomain/32/303402_2.png) [@nodomain](https://meta.discourse.org/u/nodomain)
#### Post date: [August 5, 2023, 7:52am UTC](https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026/3 "2023-08-05T07:52:53Z")

</div>

Thank you! That did the trick.

Do you also happen to know how I can get the UI language from the user’s settings?

---

<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: [September 4, 2023, 7:52am UTC](https://meta.discourse.org/t/how-can-i-trigger-my-custom-statistics-script-on-every-page-load/274026/4 "2023-09-04T07:52:57Z")

</div>

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