This guide covers how to use the JavaScript Action in Personyze to run custom scripts as part of your campaign logic. JavaScript actions allow advanced control over page behavior, dynamic content manipulation, integration with external tools, and real-time user interaction enhancements.
Overview
The JavaScript Action block executes custom JavaScript in your campaign. You can use it to:
- Modify page content dynamically
- Track additional user behaviors
- Trigger events or conversions
- Integrate with third-party analytics or CRMs
- Show or hide elements conditionally
This action is highly flexible and can work alone or in combination with banners, popups, and targeting rules.
Where to Find It
In the campaign editor, under Actions, choose: Run JavaScript Code
Key Settings
1. Code Editor
Write or paste your JavaScript directly into the code box. You can use standard JS and the Personyze API (like _set_case()
, _track_event()
, etc.).
2. Execute in Local Scope
When checked (default), the script runs inside the isolated context of the action. This is recommended to avoid variable conflicts. If your script must access global variables or third-party libraries already on the page, you can uncheck this box.
3. When to Execute
Defines when your code runs:
- When document is ready – Fires after the page has loaded (recommended)
- When action is shown – Fires only if this action is triggered
- When timer ends – Optional if you’re chaining logic with a countdown
Optional Settings
External Assets
Use this section if your script depends on an external JS or CSS file (e.g., jQuery, analytics pixel, animation library). Personyze will automatically load these assets before executing your script.
Advanced Event Trigger
If needed, you can specify advanced event conditions under “When Event Takes Place.” This allows greater control over when your JS should be run based on specific user interactions or triggers.
Don’t Show If Conditions
- If action was shown or clicked previously
- If another action was or wasn’t shown
- If a condition was already met
Common Use Cases
- Track custom events:
_track_event('email_popup_seen');
- Trigger a case change:
_set_case('b', null, false, {template: 'animator_fade'});
- Insert dynamic content:
document.getElementById('cta-message').innerText = 'Limited time offer!';
- Send event to Google Tag Manager:
dataLayer.push({event: 'personyze_popup_shown'});
- Run conditional logic:
if (window.location.href.includes('pricing')) { _set_case('discount_offer'); }
Best Practices
- Use clear, scoped variable names to avoid conflicts
- Always test JavaScript in preview before publishing
- Use console logging for debugging (
console.log()
) - If integrating with external platforms (e.g., HubSpot, GA, Meta Pixel), confirm loading timing
Resources
Need help writing or debugging custom code? Reach out to Personyze support or request a script review from our technical team.