# How to know when transaction is finished

**URL:** <https://discuss.elastic.co/t/how-to-know-when-transaction-is-finished/272587>\
**Category:** APM\
**Tags:** rum\
**Created:** [May 10, 2021, 1:38pm UTC](https://discuss.elastic.co/t/how-to-know-when-transaction-is-finished/272587 "2021-05-10T13:38:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_duke](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@the\_duke](https://discuss.elastic.co/u/the_duke)\
**Post date:** [May 10, 2021, 1:38pm UTC](https://discuss.elastic.co/t/how-to-know-when-transaction-is-finished/272587/1 "2021-05-10T13:38:03Z")

</div>

Hi All,

I was wondering how one can determine if a transaction is finished?

I am working on an angular project and want to log how long a user is logged in.

When the user logs out, I start a transaction, calculate the session duration, and then end the transaction. However, I see in my network traffic in chrome that this transaction is not always sent, because part of the logoutprocess is to redirect the user back to the login page. As far as I can tell, this prevents the transaction from completing and no data is sent to elastic.

Here is the simplified code:

```auto
     const transaction = apm.startTransaction('User authenticated logging out', 'custom');
      // some session duration calculation is happening here
    ...
      
      apm.addLabels({ sessionDuration: sessionDuration });
      transaction.end();

      this.router.navigateByUrl('/auth/login'); // sometimes prevents transaction from finishing
   ...

```

If I delay the rediction by, say, 1 second, like in the code below, then I have no issues and I can see the logout event in Kibana.

```auto
     const transaction = apm.startTransaction('User authenticated logging out', 'custom');
      // some session duration calculation is happening here
    ...
      
      apm.addLabels({ sessionDuration: sessionDuration });
      transaction.end();
      setTimeout(() => {
        this.router.navigateByUrl('/auth/login'); // transaction is always finished
      }, 1000);

```

I've searched online for a solution, but I cant seem to find people experiencing the same problem, which suggests I'm probably doing something wrong.

I did find this api call: transaction.isFInished(): boolean, but unfortunately checking for this to be true before redirecting doesnt make a difference. Also the comment on this function says:

```auto
      /**
       * undocumented, might be removed in future versions
       */
      isFinished(): boolean

```

Which is a real shame, cos it sounds exactly like what I need.

So how do I determine if a transaction is finished before executing other code?

I am using "@elastic/apm-rum-angular": "^1.1.9"

Also, I am very new to APM and this forum, so if more information is needed, please let me know.

Any help is greatly appreciated

---

<div class="post-metadata">

**Author:** ![vigneshshanmugam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vigneshshanmugam/32/40324_2.png) [@vigneshshanmugam](https://discuss.elastic.co/u/vigneshshanmugam)\
**Post date:** [May 11, 2021, 5:09pm UTC](https://discuss.elastic.co/t/how-to-know-when-transaction-is-finished/272587/2 "2021-05-11T17:09:02Z")

</div>

Hi @the_duke,

For custom transactions in general, the user is in full control of how and when to end the transaction. So in your case, the transaction is never finished and sent to the browser until you call `transaction.end` method.

The reason why you are not seeing the transaction event in Kibana is because the browser navigates away from the page and the previous transaction could be lost which explains why the event is sent when you add a timeout with `setTimeout` call. You can configure how frequent to flush the event queue using the `flushInterval` config - [Configuration | APM Real User Monitoring JavaScript Agent Reference [5.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/rum-js/current/configuration.html#flush-interval).

We are aware of this issue and trying to track all events that unloads the previous page. Please track the issue here - [Send RUM events for all activity (including partial page loads that don't reach onLoad) · Issue #970 · elastic/apm-agent-rum-js · GitHub](https://github.com/elastic/apm-agent-rum-js/issues/970) and let us know if we can help in any way.

---

<div class="post-metadata">

**Author:** ![the\_duke](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@the\_duke](https://discuss.elastic.co/u/the_duke)\
**Post date:** [May 12, 2021, 2:29pm UTC](https://discuss.elastic.co/t/how-to-know-when-transaction-is-finished/272587/3 "2021-05-12T14:29:36Z")

</div>

Hi @vigneshshanmugam ,

Thank you for your reply. I will look into the flushInterval and am subscribed to the github issue you mentioned.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 2, 2021, 10:30am UTC](https://discuss.elastic.co/t/how-to-know-when-transaction-is-finished/272587/4 "2021-06-02T10:30:30Z")

</div>

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