Identify the current user. Read more about why.

Signature

window._elev.on('load', function (_elev)  {
  _elev.setUser(user);
});

user (required)

  • user.email
    • String of user’s email address.
  • user.user_hash
    • When sending an email you MUST also send a matching user_hash . The user_hash verifies to Elevio that the email address is legitimate.
    • It should be generated as a HashMac using the user details, and your account secret which you can find here. See below for example code.
  • user.first_name
    • String of user’s first name.
  • user.last_name
    • String of user’s last name.
  • user.registered_at
    • Timestamp of when the user registered for your system (in milliseconds)
  • user.groups
    • Array of user’s groups.
  • user.traits
    • Object of user’s traits.

Examples and setting the user_hash

PHP
window._elev.on('load', function (_elev)  {
  _elev.setUser({
    email: '<?php echo $user->email; ?>',
    user_hash: '<?php echo hash_hmac("sha256", $user->email, "ADD_YOUR_SECRET_ID_HERE"); ?>',
    registered_at: <?php echo $user->registered_at; ?>
  });
});
Ruby on Rails
window._elev.on('load', function (_elev)  {
  _elev.setUser({
    email: "<%= current_user.email %>",
    user_hash: "<%= OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), "ADD_YOUR_SECRET_ID_HERE", current_user.email) %>",
    registered_at: <%= current_user.registered_at %>
  });
});
ASP.NET
# Helpers/ElevioHelper.cs
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Your.App.Helpers
{
    public static class ElevioHelper
    {
        public static string HMACSHA256HashString(string stringToHash)
        {
            string elevioSecret = "'ADD_YOUR_SECRET_ID_HERE";
            byte[] secretKey = Encoding.UTF8.GetBytes(elevioSecret);
            byte[] bytes = Encoding.UTF8.GetBytes(stringToHash);
            using (var hmac = new HMACSHA256(secretKey))
            {
                hmac.ComputeHash(bytes);
                byte[] data = hmac.Hash;

                var sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++) {
                    sBuilder.Append(data[i].ToString("x2"));
                }
                return sBuilder.ToString();
            }
        }
    }
}

Generate user_hash on every request for the currently logged in user.

window._elev.on('load', function (_elev)  {
  _elev.setUser({
    email: "<%= Membership.GetUser().Email %>",
    user_hash: "<%= Your.App.Helpers.ElevioHelper.HMACSHA256HashString(Membership.GetUser().Email) %>",
    registered_at: <%= Membership.GetUser().RegisteredAt %>
  });
});
Python
# PYTHON 2
import hmac, hashlib

elevio_secret = 'ADD_YOUR_SECRET_ID_HERE'
def generate_elevio_user_hash(user_email):
    return hmac.new(elevio_secret, user_email, digestmod=hashlib.sha256).hexdigest()

# PYTHON 3
import hmac, hashlib 

elevio_secret = 'ADD_YOUR_SECRET_ID_HERE' 
def generate_elevio_user_hash(user_email): 
    return hmac.new(elevio_secret.encode(), user_email.encode(), digestmod=hashlib.sha256).hexdigest()

Generate the user_hash on every request with the email address of the current user. If the above function was available in your templates:

window._elev.on('load', function (_elev)  {
  _elev.setUser({
    email: "${ user.email }",
    user_hash: "${ generate_elevio_user_hash(user.email) }",
    registered_at: ${ user.registered_at }
  });
});
Django

Create a new Django based template tag to help create your user_hash.

# yourapp/templatetags/elevio_user_hash.py
import hmac, hashlib
from django import template

register = template.Library()

ELEVIO_SECRET = 'ADD_YOUR_SECRET_ID_HERE'
def elevio_user_hash(user):
    return hmac.new(ELEVIO_SECRET, user.email, digestmod=hashlib.sha256).hexdigest()

register.filter('elevio_user_hash', elevio_user_hash)

Generate user_hash on every request for the currently logged in user.

{% load elevio_user_hash %}
window._elev.on('load', function (_elev)  {
  _elev.setUser({
    email: "{{ user.email !!}",
    user_hash: "{{ user.elevio_user_hash !!}",
    registered_at: {{ user.registered_at }}
  });
});

Java

import javax.crypto.*;
import javax.crypto.spec.*;
import org.apache.commons.codec.binary.*;

Mac sha256HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec("ADD_YOUR_SECRET_ID_HERE".getBytes("UTF-8"), "HmacSHA256");
sha256HMAC.init(secretKey);
String userHash = Hex.encodeHexString(sha256HMAC.doFinal(user.getEmail().getBytes("UTF-8")));

Generate user_hash on every request for the currently logged in user.

<% load elevioUserHash %>
window._elev.on('load', function (_elev)  {
  _elev.setUser({
    email: "<%= user.getEmail() %>",
    user_hash: "<%= user.getElevioUserHash() %>",
    registered_at: <%= user.getRegisteredAt() %>
  });
});

Javascript

This example uses Express with EJS templates, it should be similar to other servers/templating engines.

In your Node.js server code

var express = require("express");
var app = express();
var crypto = require("crypto");

// set the view engine to ejs
app.set("view engine", "ejs");

// index page
app.get("/", function(req, res) {
  const user = {
    email: "test@hello.com",
    firstName: "first",
    lastName: "last"
  };
  const elevioAccountSecret = "123";
  const hmac = crypto.createHmac("sha256", elevioAccountSecret);
  hmac.update(user.email);
  user.hash = hmac.digest("hex");
  res.render("pages/index", { user });
});

This puts an object named user into your template scope. Then to render in your template code

window._elev.on("load", function (_elev) {
  window._elev.setUser({
    first_name: '<%= user.firstName %>',
    last_name: '<%= user.lastName %>',
    email: '<%= user.email %>',
    user_hash: '<%= user.hash %>',
    registered_at: <%= user.registeredAt %>
  });
});