This feature is only available on the Plus and Business plan.
To see if one of our paid plans suits your site better, check out everything our Plus and Business plans have to offer in Hotjar Plans.
Using JavaScript to trigger a Feedback widget can be useful when Hotjar's targeting rules are not enough to target a specific audience. JavaScript triggers override all URL based targeting, including Excluding URLs.
These triggers could also be used if:
- You want a Survey to appear, only after your visitor has reached a specific part of the page.
- You want the Survey pop-up to appear as soon as your visitors click on a specific element.
This feature is also available for Recordings and Heatmaps.
Step By Step Guide
Select JavaScript trigger from your page targeting panel.
Enter a name you would like to use as your trigger.
You can select the '+' icon if you would like to add another targeting rule. Only alphanumeric characters (a-z, A-Z, 0-9), underscores (_), and dashes (-) can be used in the trigger name.
Enter any additional targets you'd like the Feedback widget to trigger.
Triggers will show before other targeting rules
If a Javascript trigger is present on a page, it will cause the Survey or Incoming Feedback widget to show regardless of any other rules present.
Click Done.
Add the trigger code to your website.
In the example above, you've created a trigger called test_variant_a. Next, you will need to add the trigger code to your website:
hj('trigger', 'test_variant_a');
If this JavaScript is added before the Tracking Code, there will be an error
This code must be added at any point after the Hotjar Tracking Code.
Advanced JavaScript Trigger Settings
This code is for advanced Hotjar users
If you'd like to implement any of this code, we'd suggest reaching out to your web development team to make sure installation is correct.
Trigger for When the User Completed a Specific Action
In this scenario, you would add this line of code to one of your JavaScript functions:
function newsletterSignup() { ... hj('trigger', 'signup-survey'); }
When using triggers for widgets, your behavior rules will still be followed. As an example, if you set a Survey to open after 10 seconds and then use a JavaScript trigger, the Survey will appear 10 seconds after triggering it with the code above.
Trigger for if the Code Will Run Before the Hotjar Script is Loaded
In this scenario, you would need to add this line of code right before your trigger code:
window.hj=window.hj||function(){(hj.q=hj.q||[]).push(arguments)};
You can also set up JavaScript Triggers through Google Tag Manager.
If you use Google Tag Manager to load the Hotjar script on your site, you can set up JavaScript Triggers on the GTM tag that fires the Hotjar tracking code. Check out this guide on how to set up JavaScript Triggers in GTM.