# GoSquared People integration - how to get user name and email in Javascript at \<head\> customization?

**URL:** https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917
**Category:** Support
**Created:** [27 مايو 2016، 6:48م UTC](https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917 "2016-05-27T18:48:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jbruni](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jbruni/32/106798_2.png) [@jbruni](https://meta.discourse.org/u/jbruni)
#### Post date: [27 مايو 2016، 6:48م UTC](https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917/1 "2016-05-27T18:48:23Z")

</div>

- I’m setting up a fresh new Discourse forum.

- I’ve already copy/pasted [GoSquared](https://www.gosquared.com/) tracking code into the `</head>` section (at `/admin/customize/css_html/1/body-tag`)

- Now, for their “People” feature, they provide the code snippet below:

```plaintext
  _gs('identify', {
    // TODO User ID
    // id: 123,

    // TODO User's full name
    // name: 'Jony Ive',

    // TODO User's email address
    // email: 'jony@apple.com',

    // TODO User's signup date
    // created_at: '2015-01-01 00:00:00'
  });

```

- I am supposed to edit the code above, providing user’s **id** , **name** , **email** and **created\_at** properties.

- **How can I do this?** Please, help.

- In fact, I was able to grab the user **id** by using the awful hack below, in the `</body>` section. It grabs the `userId` used by Google Analytics:

```plaintext
<script>
  // GoSquared People
  if (ga && ga.q && ga.q[0] && ga.q[0][2] && ga.q[0][2].userId) {
    _gs('identify', { id: ga.q[0][2].userId });
  }
</script>

```

- So far, so good. How can I do this properly, and adding **name** , **email** and **created\_at**?

- Note: I am using `[Communiteq](https://www.communiteq.com) (formerly DiscourseHosting)`. So, I do not have access to the Discourse installation/system. I only have access to the user interface.

Thank you!

---

<div class="post-metadata">

### Author: ![Chopper](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chopper/32/57073_2.png) [@Chopper](https://meta.discourse.org/u/Chopper)
#### Post date: [29 مايو 2016، 11:18ص UTC](https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917/3 "2016-05-29T11:18:44Z")

</div>

I will do something like this :

1. get username with the current session
2. get the rest now you have the username with the API

* * *

```javascript
$(document).ready(function() {
    $.ajax({
        type: 'GET',
        cache: false,
        url: 'https://discoruse.forum.com/session/current.json',
        dataType: 'json',
        xhrFields: {
            'withCredentials': true
        },
        success: function(result){
            // you get the username
            result = result.current_user;
            var user_name = result.username;
            var user_id = result.id;
            var user_avatar = result.avatar_template;
            $.ajax({
                type: 'GET',
                cache: false,
                url: 'https://forum.com/users/' + user_name + '/activity.json',
                dataType: 'json',
                xhrFields: {
                    'withCredentials': true
                },
                success: function(result){
                    // you get the email and date of registration
                    result = result.user;
                    var user_email = result.email;
                    var user_registation = result.created_at

                },
                error: function(result){
                    console.log('Error: '+result);
                }
            });
        },
        error: function(result){
            console.log('Error: '+result);
        }
    });

});

```

* * *

You can insert this code into the body from the admin panel → cutomization

---

<div class="post-metadata">

### Author: ![jbruni](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jbruni/32/106798_2.png) [@jbruni](https://meta.discourse.org/u/jbruni)
#### Post date: [30 مايو 2016، 12:12ص UTC](https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917/5 "2016-05-30T00:12:34Z")

</div>

It worked. I’ve done a few customizations, added some fields. I wish we could do it with no extra HTTP calls… Anyway, it is up & running, and it works. 👍

---

<div class="post-metadata">

### Author: ![TheDeveloper](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thedeveloper/32/121362_2.png) [@TheDeveloper](https://meta.discourse.org/u/TheDeveloper)
#### Post date: [3 يونيو 2016، 4:15م UTC](https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917/6 "2016-06-03T16:15:45Z")

</div>

Hi, Geoff from [GoSquared](https://gosquared.com) here.

We use Discourse ourselves and have of course integrated GoSquared with it.

Thanks to @Chopper for the good example on identifying with user properties. We’ve built on this approach and come up with a snippet of code which also captures the visitor’s pageviews as they browse around the discourse site:

```html
<script type="text/discourse-plugin" version="0.4">
  !function(g,s,q,r,d){r=g[r]=g[r]||function(){(r.q=r.q||[]).push(
  arguments)};d=s.createElement(q);q=s.getElementsByTagName(q)[0];
  d.src='//d1l6p2sc9645hc.cloudfront.net/tracker.js';q.parentNode.
  insertBefore(d,q)}(window,document,'script','_gs');

  // replace GSN-your-token with your site token
  _gs('GSN-your-token', false);

  api.onPageChange((path, title) => _gs('track', path, title));

  $(document).ready(() => {
    const currentUser = api.getCurrentUser();
    if (!currentUser) return;

    $.ajax({
      type: 'GET',
      cache: false,
      url: `/users/${currentUser.username}/activity.json`,
      dataType: 'json',
      xhrFields: {
        'withCredentials': true
      },
      success: result => {
        const user = result.user;
        // map discourse props to GS special props
        const map = {
          id: 'id',
          email: 'email',
          name: 'name',
          username: 'username',
          created_at: 'created_at'
        };

        const props = {};
        const custom = {};

        for (let p in user) {
          let gsName = map[p];
          if (gsName) props[gsName] = user[p];
          else custom[p] = user[p];
        }

        props.custom = custom;

        _gs('identify', props);
      }
    });
  });
</script>

```

Make sure you place your actual site token in the `_gs()` function call. Also remove any existing GoSquared integration code otherwise you might double-track.

This snippet uses the [client plugin API](https://meta.discourse.org/t/using-the-pluginapi-in-site-customizations/41281) to listen for page navigation events and trigger GoSquared pageviews. It also pulls the username from existing state to cut out one of the ajax calls.

This is something we’ve put together after a cursory look into Discourse’s APIs, there may well be a better way. If there is, we’d love to hear it!

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [1 فبراير 2022، 4:18ص UTC](https://meta.discourse.org/t/gosquared-people-integration-how-to-get-user-name-and-email-in-javascript-at-head-customization/44917/9 "2022-02-01T04:18:51Z")

</div>


