Skip to main content
Skip table of contents

RPI Web Client - Preserving anonymous profile information

Overview

Certain devices may exist in a shared environment, such as an iPad in a family’s living room or a computer at a library. As such, organizations may not want to merge any anonymous information into the known profile. This page contains two functions that can be used with the rpiWebClient to preserve and restore (or create) an anonymous profile upon a user completing a “login” action.

Code

CODE
function loginMasterKey(masterKey=null,masterKeyParameter=null,preserveAnonId=false) {
  if (!masterKey||!masterKeyParameter) {return false}
  //force merging off
  rpiWebClient.config.visitorProfileMergeMode=0
  if (preserveAnonId) {
      const cookie=JSON.parse(rpiWebClient.getCookie(rpiWebClient.config.clientId))
      if (cookie && cookie.profile && cookie.profile.IsMasterKey==false) {
          //only save old cookie if it is actually anon; would only matter if using query string parameters or something like that to trigger this
          const days=rpiWebClient.config.visitorCookieDuration
          //create anonymous cookie
          rpiWebClient.setCookie(rpiWebClient.config.clientId+"-anon", JSON.stringify(rpiWebClient.visitor), days, rpiWebClient.config.realtimeCookieDomain, rpiWebClient.config.realtimeCookieSameSite, true);
      }
  }
  //setting new visitorId to the masterkey. Have to disable profile merging or else the anonymous profile will be tied to the master
  rpiWebClient.visitor.profile.VisitorID=masterKey
  rpiWebClient.realtimeParameters.push({Name:masterKeyParameter,Value:masterKey})
  rpiWebClient.submitVisitorDetails()
  rpiWebClient.pushWebEvent("Login","1")
  return true
  }

function logoutMasterKey(restoreAnonId=false) {
    rpiWebClient.pushWebEvent("Logout","1")
    if (restoreAnonId) {
        const anonCookie=JSON.parse(rpiWebClient.getCookie(rpiWebClient.config.clientId+"-anon"))
        if (anonCookie && anonCookie.profile) {
            const anonVisitor=anonCookie.profile.VisitorID
            if (anonVisitor!==undefined &&anonVisitor!==rpiWebClient.visitor.profile.VisitorID) {
                //logout event happens under master key, then a new visit is sent for the anonymous Id
                rpiWebClient.visitor.profile.VisitorID=anonVisitor
                rpiWebClient.submitVisitorDetails()
                //only early return if there is an anon profile to restore.
                //otherwise create a new anon profile.
                return true
            }
        }
    }
    rpiWebClient.visitor.profile.VisitorID=null
    rpiWebClient.submitVisitorDetails()
    return true
    //creates new anon profile
}

Usage

loginMasterKey

The loginMasterKey function should be called when a user authenticates themselves. masterKey will be stored as the ID of the visitor profile in the cache. masterKey should be the external ID you wish to use to identify this user across systems, for instance an Okta ID. It will also be saved in the cache as the parameter defined by masterKeyName. Additionally a login event will be logged for the profile. Enabling the preserveAnonId will store the anonymous visitor profile. It can then later be re-set as the active profile using logoutMasterKey.

A sample implementation is below:

CODE
document.getElementById("login-button").addEventListener('click',
            (e)=>{
                //get masterkey value 
                const masterKey=document.getElementById("masterKey-input").value
                loginMasterKey(masterKey,"masterKeyName")
                //Complete other login actions
            }
        )

logoutMasterKey

logoutMasterKey sets the visitor profile for a browser to an anonymous profile. If the restoreAnonId is true, then it will check for a previously saved anonymous visitor profile from loginMasterKey. If one cannot be found or if restoreAnonId is false, then a new anonymous visitor profile will be created. A logout event will always be logged for the active profile when this function is called. This should be called any time the user is logged out of their session (e.g., inactivity timeout or clicking a logout button).

A sample implementation is below:

CODE
document.getElementById("logout-visitor").addEventListener('click',
            (e)=>{
                //log masterkey out
                logoutMasterKey()
            }
        )
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.