# How to use apm.setUserContext in Angular with the logged in user info

**URL:** <https://discuss.elastic.co/t/how-to-use-apm-setusercontext-in-angular-with-the-logged-in-user-info/256110>\
**Category:** APM\
**Tags:** rum\
**Created:** [November 20, 2020, 11:49am UTC](https://discuss.elastic.co/t/how-to-use-apm-setusercontext-in-angular-with-the-logged-in-user-info/256110 "2020-11-20T11:49:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterDK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterdk/32/49432_2.png) [@PeterDK](https://discuss.elastic.co/u/PeterDK)\
**Post date:** [November 20, 2020, 11:49am UTC](https://discuss.elastic.co/t/how-to-use-apm-setusercontext-in-angular-with-the-logged-in-user-info/256110/1 "2020-11-20T11:49:05Z")

</div>

**Kibana version** : 7.9.2

**Elasticsearch version** : 7.9.2

**APM Server version** : 7.9.2

**APM Agent language and version** : Angular 8

Hi,

I'm trying to use the Angular Rum agent for a frontend application.  
It's working and sending transaction information to APM.  
Now, I want to add the user information of the logged in user to the transactions with apm.setUserContext.  
I'm able to do this with hardcoded variables, but when I try to get the info from a function it's empty.

This is my working app.module.ts part

```
export class AppModule {

  userContextId = '666';
  userContextName = 'TestUserName';
  userContextEmail = 'testemail@foo.com';

  constructor(@Inject(ApmService) service: ApmService){

    const apm = service.init({
      serviceName: 'Frontend Local',
      environment: 'Local',
      serverUrl: constants.apmURL,
      distributedTracingOrigins: [constants.distributedTracingOrigins],
      serviceVersion: version
    })

    apm.setUserContext({
      'id': this.userContextId,
      'username': this.userContextName,
      'email': this.userContextEmail
    })
  }
 }

```

How do I send real info instead of the test info?  
I have an authService where the userprofile info is available, but I can't get this into the apm events.  
For example with:  
getUserName() {  
return this.\_authService.authContext.userProfile.name  
}

Even with the following very simple test it doesn't work:  
getUserName() {  
return 'MyName'  
}  
With of course:  
apm.setUserContext({  
'id': this.userContextId,  
'username': this.getUserName,  
'email': this.userContextEmail  
})

Kind regards,  
Peter

---

<div class="post-metadata">

**Author:** ![Hamidreza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hamidreza/32/21369_2.png) [@Hamidreza](https://discuss.elastic.co/u/Hamidreza)\
**Post date:** [November 20, 2020, 2:56pm UTC](https://discuss.elastic.co/t/how-to-use-apm-setusercontext-in-angular-with-the-logged-in-user-info/256110/2 "2020-11-20T14:56:08Z")

</div>

Hi @PeterDK,

Thanks for reaching out.

Your questions seems to be related to Angular rather than to the elastic APM. But you should be able to inject your AuthService into the AppModule and use that to configure the user information. Please see [Angular's dependency injection guide](https://angular.io/guide/dependency-injection) for more detail.

Hope this helps,  
Hamid

---

<div class="post-metadata">

**Author:** ![PeterDK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterdk/32/49432_2.png) [@PeterDK](https://discuss.elastic.co/u/PeterDK)\
**Post date:** [November 20, 2020, 3:13pm UTC](https://discuss.elastic.co/t/how-to-use-apm-setusercontext-in-angular-with-the-logged-in-user-info/256110/3 "2020-11-20T15:13:11Z")

</div>

That's what I tried, but that doesn't seem to work. On some point I will need to call a function for that, but even with a simple function it does not get the info.

I tried this again:

```
apm.setUserContext({
      'id': '666',
      'username': this.getUserName,
      'email': 'testemail@foo.com'
    })
  }

  getUserName() {
    return 'My user name'
  }

```

I see this in the events:  
[user.email:testemail@foo.com](mailto:user.email:testemail@foo.com)  
user.id:666

But user.name is missing.

Or I'm I just doing something stupid 🤔

---

<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:** [December 11, 2020, 11:13am UTC](https://discuss.elastic.co/t/how-to-use-apm-setusercontext-in-angular-with-the-logged-in-user-info/256110/4 "2020-12-11T11:13:12Z")

</div>

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