Google Analytics UserId in Hubspot

May 12, 2017 | 1 minute read

Paste this below your Analytics tracking code, in a template or content settings footer field:

<script>
  ga('set', 'userId', '{% raw %}{{contact.email|md5}}{% endraw %}');
</script>

How does it work?

User ID enables the analysis of groups of sessions, across devices, using a unique, persistent, and non-personally identifiable ID string representing a user.

The easiest solution would be of course to identify visitors by their e-mail address, but we are not allowed to do that - as you can identify person using their e-mail.

Instead, we can use HubL and its md5 filter. md5 is a hash function which creates an unique string of characters from another string. For example, Hello World! becomes 86fb269d190d2c85f6e0468ceca42a20. Generated string is always the same for the same source, which means every time we will receive the same 86fb269d190d2c85f6e0468ceca42a20 hash from Hello World!.

{% raw %}{{contact.email|md5}}{% endraw %} creates md5 hash from current visitor’s e-mail address, which we can use as a safe, unique identifier for Google Analytics. Different e-mails will generate unique hashes, and these hashes will stay the same for every visitor.

I write one post every few months. I can send you an email when I publish something new.