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

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

  • I’ve already copy/pasted GoSquared 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:

  _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:

<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!

1 Like

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

$(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

3 Likes

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. :thumbsup:

2 Likes

Hi, Geoff from GoSquared 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:

<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 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!

4 Likes