# Login Event Listener

**URL:** https://meta.discourse.org/t/login-event-listener/211653
**Category:** Support
**Created:** [December 9, 2021, 8:00pm UTC](https://meta.discourse.org/t/login-event-listener/211653 "2021-12-09T20:00:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aliparsifar](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@aliparsifar](https://meta.discourse.org/u/aliparsifar)
#### Post date: [December 9, 2021, 8:00pm UTC](https://meta.discourse.org/t/login-event-listener/211653/1 "2021-12-09T20:00:51Z")

</div>

Hi,

I’m trying to track user logins on our client’s forum. Basically, I’d like to create an event listener (in vanilla JS) that runs a JavaScript function every time a user logs in to the forum.

Is there a DOM event that I can listen for?

I’m trying to do this in Google Tag Manager.

Thanks.

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [December 10, 2021, 12:31pm UTC](https://meta.discourse.org/t/login-event-listener/211653/2 "2021-12-10T12:31:49Z")

</div>

You can use something like this in the `common > header` tab of a theme component.

```xml
<script type="text/discourse-plugin" version="0.8">
  const user = api.getCurrentUser();

  // user is already logged in, bail
  if (user) {
    return;
  }

  // do your work here...
  const clickCallback = (clickEvent) => {
    console.log(clickEvent);
    alert("user is logging in");
    // ..
    // ..
  };

  api.onAppEvent("modal:body-shown", (event) => {
    const loginButton = document.querySelector("#login-button");

    if (loginButton) {
      loginButton.addEventListener("click", clickCallback, { once: true });
    }
  });
</script>

```

Add whatever code you need in the `clickCallback` function, and it will fire when the user clicks on the Login button inside the modal. That id is currently missing on mobile, but that will be fixed when [this](https://github.com/discourse/discourse/pull/15255) is merged

---

<div class="post-metadata">

### Author: ![aliparsifar](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@aliparsifar](https://meta.discourse.org/u/aliparsifar)
#### Post date: [December 13, 2021, 7:52pm UTC](https://meta.discourse.org/t/login-event-listener/211653/3 "2021-12-13T19:52:51Z")

</div>

> [@Johani](#):
>
> `const loginButton = document.querySelector("#login-button");`

Thanks so much for the response, Joe!
