Element inference is how Userflow.js remembers an element you selected in your app, such as the target of a tooltip or a launcher, so it can find the same element again later. If your app's class names or IDs change every time you deploy, Userflow.js can lose track of the element. Your developers can tell Userflow.js which class names and IDs to ignore, using the code on this page.
What is element inference in Userflow?
Element inference is the process Userflow.js uses to recognize an element you selected in your app. When you select an element, Userflow.js records information about it, such as its class names, its ID and other attributes, and uses that information to find the same element on later page loads.
If those class names or IDs are generated by your app and change with each deploy, the recorded information no longer matches, and Userflow may show an Element not found message. A tooltip that worked before and stopped after a new release of your app is a common sign.
Before changing code, you can often fix a single element in the flow builder: adjust strictness, or use Go manual with a stable attribute such as data-testid. See Selecting elements. The settings on this page fix the problem for every element in your app at once.
Each element inference setting goes in your app's code, before the line that calls userflow.init().
How do I make Userflow.js ignore dynamic class names?
Use userflow.setInferenceClassNameFilter to tell Userflow.js which class names to ignore. For example, a CSS-in-JS library might generate class names in the format css-<hash>, where <hash> changes with every deploy.
Add this line before you call userflow.init():
userflow.setInferenceClassNameFilter(cls => !cls.startsWith('css-'))You can also use a negative lookahead regular expression that only matches class names that don't start with css-:
userflow.setInferenceClassNameFilter(/^(?!css-).*/)To filter by more than one pattern, or to use functions instead of regular expressions, see userflow.setInferenceClassNameFilter in the Userflow.js reference.
If all of your app's class names are dynamic, you can tell Userflow.js to ignore every class name with a filter that always returns false:
userflow.setInferenceClassNameFilter(() => false)Only do this if none of your class names are stable. Ignoring useful, non-dynamic class names makes it harder for Userflow to find elements.
How do I make Userflow.js ignore dynamic IDs?
By default, Userflow.js uses an element's id attribute, unless the ID ends in a number. Userflow.js skips those IDs so that it works out of the box with frameworks such as Ember.js, which give elements IDs like ember123.
If your app generates IDs in a different pattern, use userflow.setInferenceAttributeFilter. Add this line before you call userflow.init():
userflow.setInferenceAttributeFilter('id', id => !id.startsWith('auto-'))This example tells Userflow.js to ignore all IDs that start with auto-. For details, see userflow.setInferenceAttributeFilter in the Userflow.js reference.
Which element attributes does Userflow.js use, and how do I change them?
By default, Userflow.js may use the values of these element attributes to recognize an element:
data-fordata-iddata-testiddata-test-idforidnameplaceholderrole
To use a different set of attributes, call userflow.setInferenceAttributeNames with the list you want. Add this line before you call userflow.init():
userflow.setInferenceAttributeNames(['name', 'placeholder', 'data-testid'])For details, see userflow.setInferenceAttributeNames in the Userflow.js reference.
If you'd like help configuring element inference for your app, contact us.