If your app already uses Google Tag Manager (GTM), you can install Userflow.js as a GTM tag instead of adding code to your app. For other ways to install, see Userflow.js Installation.
What you need before installing Userflow.js with Google Tag Manager
Your Google Tag Manager account needs a variable that holds the signed-in user’s ID. You can also use variables for other attributes, such as the user’s name, email, or sign-up date.
This guide assumes your user ID variable is named userId. If yours has a different name, replace {{userId}} in the snippet with your variable’s name.
Add the Userflow.js tag in Google Tag Manager
Sign in to Userflow.
Go to Settings > Environments.
Copy your Userflow.js Token. It starts with
ct_.Sign in to your Google Tag Manager account.
Go to Tags.
Click New.
Click Tag Configuration.
Click Custom HTML.
In the HTML field, paste this snippet, and replace
USERFLOW_TOKENwith the token you copied in step 3:<script> !function(){var e="undefined"==typeof window?{}:window,t=e.userflow,r=e.history;function n(t,n){return function(){var o=new CustomEvent(n),s=Array.prototype.slice.call(arguments),a=t.apply(r,s);return e.dispatchEvent(o),a}}if(r){e.__userflowStatePatched=!0;var o=r.pushState,s=r.replaceState;r.pushState=n(o,"userflow:pushstate"),r.replaceState=n(s,"userflow:replacestate")}if(!t){var a="https://js.userflow.com/",l=null;t=e.userflow={_stubbed:!0,load:function(){return l||(l=new Promise((function(t,r){var n=document.createElement("script");n.async=!0;var o=e.USERFLOWJS_ENV_VARS||{};"es2020"===(o.USERFLOWJS_BROWSER_TARGET||function(e){for(var t=[[/Edg\//,/Edg\/(\d+)/,80],[/OPR\//,/OPR\/(\d+)/,67],[/Chrome\//,/Chrome\/(\d+)/,80],[/CriOS\//,/CriOS\/(\d+)/,100],[/Safari\//,/Version\/(\d+)/,14],[/Firefox\//,/Firefox\/(\d+)/,74]],r=0;r<t.length;r++){var n=t[r],o=n[0],s=n[1],a=n[2];if(e.match(o)){var l=e.match(new RegExp(s));if(l&&parseInt(l[1],10)>=a)return"es2020";break}}return"legacy"}(navigator.userAgent))?(n.type="module",n.src=o.USERFLOWJS_ES2020_URL||a+"es2020/userflow.js"):n.src=o.USERFLOWJS_LEGACY_URL||a+"legacy/userflow.js",n.onload=function(){t()},n.onerror=function(){document.head.removeChild(n),l=null;var e=new Error("Could not load Userflow.js");console.warn(e.message),r(e)},document.head.appendChild(n)}))),l}};var u=e.USERFLOWJS_QUEUE=e.USERFLOWJS_QUEUE||[],i=function(e){t[e]=function(){var r=Array.prototype.slice.call(arguments);t.load(),u.push([e,null,r])}},c=function(e){t[e]=function(){var r,n=Array.prototype.slice.call(arguments);t.load();var o=new Promise((function(e,t){r={resolve:e,reject:t}}));return u.push([e,r,n]),o}},d=function(e,r){t[e]=function(){return r}};i("_setTargetEnv"),i("closeResourceCenter"),i("disableEvalJs"),i("init"),i("off"),i("on"),i("prepareAudio"),i("registerCustomInput"),i("remount"),i("reset"),i("setBaseZIndex"),i("setCustomInputSelector"),i("setCustomNavigate"),i("setCustomScrollIntoView"),i("setInferenceAttributeFilter"),i("setInferenceAttributeNames"),i("setInferenceClassNameFilter"),i("setResourceCenterLauncherHidden"),i("setScrollPadding"),i("setServerEndpoint"),i("setShadowDomEnabled"),i("setPageTrackingDisabled"),i("setUrlFilter"),i("setLinkUrlDecorator"),i("openResourceCenter"),i("toggleResourceCenter"),c("endAll"),c("endAllFlows"),c("endChecklist"),c("group"),c("identify"),c("identifyAnonymous"),c("start"),c("startFlow"),c("startWalk"),c("track"),c("updateGroup"),c("updateUser"),c("debugger"),d("getResourceCenterState",null),d("isIdentified",!1)}}(); userflow.init('USERFLOW_TOKEN') userflow.identify('{{userId}}', { // Add user attributes here }) </script>Click Triggering.
Click All pages, or a more specific trigger if you have one.
Only fire this tag for signed-in users. Every user the tag identifies counts toward your MAU total, whether or not they ever see Userflow content. If you pick the All pages trigger and the tag also runs on public pages, such as your marketing site or sign-up page, those visitors can be counted too. Use a trigger that fires only when the
userIdvariable has a value, or only on pages inside your app. See How Userflow Tracks and Bills Monthly Active Users (MAUs).Click Save and give the tag a name, such as “Userflow”.
Send user attributes from Google Tag Manager
You can send any attributes you want to Userflow in the userflow.identify() call. Use them to target flows to specific users, for example to show a welcome modal only to users who signed up recently.
If your Google Tag Manager account has variables for the user’s name, email, or sign-up date, add them to the second argument of userflow.identify():
userflow.identify('{{userId}}', {
name: '{{name}}',
email: '{{email}}',
signed_up_at: '{{signed_up_at}}'
})This example expects the three variables to be named name, email, and signed_up_at in Google Tag Manager. The signed_up_at value must be in ISO 8601 format, for example 2021-03-26T12:31:47Z.