---
title: "Custom trigger a popup or flyout form"
description: "Set-up and create a a popup or flyout form using Klaviyo client side APIs for custom triggering forms on your website."
url: "https://developers.klaviyo.com/en/docs/how_to_custom_trigger_a_popup_or_flyout_form"
category: "Personalize messages"
lastUpdated: "2026-11-06"
related: ["https://developers.klaviyo.com/en/docs/llms.txt"]
---

# Custom trigger a popup or flyout form

> This is the Markdown version of https://developers.klaviyo.com/en/docs/how_to_custom_trigger_a_popup_or_flyout_form

Set-up and create a a popup or flyout form using Klaviyo client side APIs for custom triggering forms on your website.

## Before you begin

1. To enable Klaviyo forms, including custom triggered forms, you must have Klaviyo’s onsite tracking JavaScript, known as Klaviyo.js, installed on your site. This is the same snippet that enables Klaviyo’s _Active on Site_ tracking. Learn how to <a href="https://help.klaviyo.com/hc/en-us/articles/115005076767-Guide-to-Klaviyo-Web-Tracking#prerequisites1" target="_blank">install Klaviyo.js for your ecommerce platform</a>.
2. Before starting, <a href="https://help.klaviyo.com/hc/en-us/articles/360026474752-Guide-to-Creating-a-Signup-Form" target="_blank">create your signup form</a> in Klaviyo. 
3. Set your _Form Type_ to **Popup** or **Flyout**. Embedded forms cannot be custom triggered. 
4. In the **Targeting & Behavior** tab, under **Display Timing**, select the option _Only show on custom trigger_. 

> [!NOTE]
> If you choose a different **Display Timing** setting, the custom trigger code will still work; the form will open on whatever trigger comes first. Additionally, if you choose other targeting settings for your form, such as **By Visitor** or **By URL**, the custom trigger form will override them once its trigger is met.

<img src = "https://files.readme.io/1a6f64c-targeting.png" class width = "smart" alt = "Targeting and Behavior tab in Klaviyo forms builder with Display Timing option Only show on custom trigger selected">

5. Click <b>Publish</b>.

## Access the open form code

1. Open your signup form in Klaviyo
2. Click **Targeting & Behavior**
3. Scroll down to the _Custom Trigger_ section and copy the code snippet that appears there, as shown below  
   `window._klOnsite = window._klOnsite || [];`  
   `window._klOnsite.push(['openForm', 'FormID']);`  

The FormID will be the ID of your form, which can be found at the end of the form editor URL.

> [!NOTE]
> **It is important to note the following:**
> - Your form must be live in order for it to open. 
> - When a form has an ongoing A/B test, this API simply triggers the form’s display. It is your A/B testing logic, which is separate, that determines which variation displays. If you have an A/B test where a variation of your form has a custom trigger, you will still need to add custom trigger code to your site.

## Customize the open form code

You can choose to use the function within the code you copied from Klaviyo, `_klOnsite.push`, to open your form, or you can replace it with the function `_klOnsite.openForm` instead, depending on the needs of the trigger you select.  

- `_klOnsite.openForm` will only execute if Klaviyo.js has loaded on your site. Otherwise, it will fail. `_klOnsite.openForm` should only be used when you want to trigger a form based on user input, such as a button click (not a trigger that requires execution immediately on page load). 

  - `_klOnsite.openForm` requires one parameter, which is your Form ID. Your code will look like this:  
    `_klOnsite.openForm(['FormID'])`

- `_klOnsite.push` can execute before Klaviyo.js loads (though Klaviyo.js still needs to be installed on your site). 

  - `_klOnsite.push` requires two parameters: openForm will be the first parameter, and your Form ID will be the second parameter. Your code will look like this:  
    `_klOnsite.push(['openForm', 'FormID'])`

You can add an optional extra parameter as a callback that will execute when your form triggers. This extra parameter can be added to either the `.push` or the `.openForm` call. 

For example, we’ve added a logging parameter here:

`_klOnsite.openForm(['FormID', () => console.log('you triggered the form!')])`

## Add your form trigger

```prompt Add a custom trigger for a Klaviyo form
Add a custom trigger to this site that opens a Klaviyo popup or flyout form.

First read https://developers.klaviyo.com/en/docs/how_to_custom_trigger_a_popup_or_flyout_form.md.

Ask me for these before you change anything, unless I've already given them:
- The form's ID. It's the end of the form editor's URL in Klaviyo.
- What should open the form: for example, a click on a particular button, or a page load for some visitors.

Then:
1. Check that Klaviyo.js is on the site: a script from https://static.klaviyo.com/onsite/js/klaviyo.js?company_id=... or https://static.klaviyo.com/onsite/js/PUBLIC_API_KEY/klaviyo.js. If it isn't there, stop and tell me, because forms need it.
2. Add the trigger where the site keeps its other scripts and event handlers, following its conventions. Use `window._klOnsite = window._klOnsite || []; window._klOnsite.push(['openForm', '<form ID>']);`, which works before Klaviyo.js has loaded. Only use `window._klOnsite.openForm(['<form ID>'])` inside a handler for something the visitor does, such as a click.
3. Don't change anything about the form itself; that's done in Klaviyo.

When you're done, tell me what you changed and how to test it. Remind me that the form only opens if it's live in Klaviyo and its type is Popup or Flyout.
```

In order to trigger your form, you’ll need to add a custom script to your website leveraging the form opening code. 

Your form trigger can be whatever you’d like, such as a button click, or on page load, but only for specific visitors based on their properties. 

In the next section, we’ll provide an example of using a button to trigger your form. 

## Example: button-triggered form

You can trigger your form open via a button click. 

In this example, our button code looks like this:

`<button class="klaviyo_form_trigger">Click here<button>`

You can always customize your trigger code to fit a pre-existing button, or customize our example button code to add a new button to your site. 

Set up your script and add an event listener that uses document.querySelector, document.getElementsByClassName, document.getElementByID, or similar to watch the button for a click. Here is our example script:

```javascript
<script type="text/javascript">
document.querySelector(".klaviyo_form_trigger").addEventListener('click',function (){
		window._klOnsite = window._klOnsite || [];
 		window._klOnsite.openForm(['FormID', () => console.log("you triggered the form!")]);
	});</script>
```

> [!NOTE]
> Note that if you add multiple forms triggered by separate buttons to your site, you’ll need to use a unique button ID or class name for each button (e.g., klaviyo\_form\_trigger1, klaviyo\_form\_trigger2).

## Test your trigger 

Once you’ve saved all the changes to your site’s code, visit your site and test your form’s trigger. When you test this code, the signup form will appear.

Having trouble? Head to Klaviyo’s <a href="https://community.klaviyo.com/" target="_blank">Community</a> for guidance from other Klaviyo users. 

## Additional resources

- <a href="https://help.klaviyo.com/hc/en-us/articles/360026474752-Guide-to-Creating-a-Signup-Form" target="_blank">Getting started with signup forms</a> 
- [Track Klaviyo form activity using JavaScript](https://developers.klaviyo.com/en/docs/track_klaviyo_form_activity_using_javascript.md)
