[How-to] Use DTR and Track Which Button Opens a Lightbox

  • 27 December 2016
  • 13 replies
  • 100 views

Userlevel 6
  • Former Unbouncer
  • 198 replies


Do you have a form in a lightbox on your page with multiple buttons set up to open it?

Maybe you want to track which button someone clicked to open the form and include that in your lead data? Or maybe you want to change the messaging in a lightbox based on the specific button clicked to open it? Now you can with a little bit of javascript! 🖥️

You can also see this in action (built in Unbounce) here:
http://unbouncepages.com/lightbox-button-pre-fill/


How to Install in Unbounce

Click Here for Instructions

🚨
This is not an official Unbounce feature. This functionality is based entirely on third party code, and has only been tested in limited applications. Since this isn’t a supported Unbounce feature, our support team will be unable to assist if things go awry. So if you are not comfortable with HTML, Javascript and CSS, please consider consulting an experienced developer.


Step 1: Add the Script

Add this Javascript to the page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
/*
Unbounce Community :: Tips & Scripts :: Use DTR and Track Which Button Opens a Lightbox 
TS:0002-04-057
***********************
Do not remove this section. It helps our team track useage of external workarounds.
*/	
	
  var params = window.location.search;
  var character = "&";
  if (params == ""){
	character = "?";	
  }
$("#lp-pom-button-322").click(function() { // Add Id of button
  var url = $(this).attr('href').split("?")[0];
  $(this).attr("href", url + params + character + "plan=Bronze"); // add parameters to pass through to lightbox
});
  
$("#lp-pom-button-323").click(function() { // Add Id of button
 var url = $(this).attr('href').split("?")[0]
 $(this).attr("href", url + params + character + "plan=Silver"); // add parameters to pass through to lightbox
});
  
$("#lp-pom-button-324").click(function() {  // Add Id of button
 var url = $(this).attr('href').split("?")[0]
 $(this).attr("href", url + params + character + "plan=Gold"); // add parameters to pass through to lightbox
});
  
$("#lp-pom-button-325").click(function() {  // Add Id of button
 var url = $(this).attr('href').split("?")[0]
 $(this).attr("href", url + params + character + "plan=Platinum"); // add parameters to pass through to lightbox
});
</script>

Make note of the ID’s for you lightbox buttons as they will need to be added to the script.

Step 2: Edit the Script

The javascript works by adding URL parameters to the URL for the lightbox behind the scenes. By doing this we can take advantage of the existing functionality in Unbounce of passing the value of a parameter as the value of a field with the same name. By using URL parameters to pass this information you can also utilize Dynamic Text replacement to change the copy in the lightbox.

You’ll need an instance of the javascript for each button. Then edit the existing ID with the ID of a specific lightbox button on your page:

Next you’ll need to edit the parameters you wish to add to the button. These can be edited in the script where it says “plan=bronze”:

The parameters can be anything you like. Separate multiple parameters with an “&” symbol. For example:
"plan=bronze&param=value&param2=value2"

Step 3: Pass the data

Make sure you have a field in your form to capture the parameter you are adding with the lightbox button. In the example I am using the parameter “plan”, so I have a field in my form that has an ID of “plan”. The same goes for any dynamic text. Make sure the parameter assigned to the text matches the parameter you added in step 2.

Step 4: Celebrate

There is no step 4 really. You’re done! Go kick back and relax or run some tests 😎


Can’t see the instructions? Log-in or Join the Community to get access immediately. 🚀


Want to take your Unbounce landing pages + Convertables™ to the next level?
:spinbounce: Check out the Ultimate List of Unbounce Tips, Scripts & Hacks


13 replies

Userlevel 7
Badge +1

This is AWESOME!! Nice one @Rob you’re a champ! 👊

This is seriously solid stuff, @Rob! Awesome job. :spinbounce:

Okay two questions:

  1. I’m trying to use this with a radio button. It SHOULD work with a radio button, No?
  2. My value has embedded spaces and a “/” - do I need to do the “%2C+” etc substitutions in the script or will that be taken care of by the protocol

So just to be clear on that last point:

I have something like “plan=Top Rockin Gold” as my param/value pair
Do I need to provide it as “plan=Top%2C+Rockin%2C+Gold” ??

Userlevel 6
Badge +4

Hi @Rob - I use this script all the time! Thank you 😊

I am also using another script to assign a specific class to buttons on a page. I do this to set a popup to trigger on that class in cases where I have more than one call to action leading to, say a form in the shape of a popup.

Let’s say I want to assign class “lightbox” to all buttons on a page and set the popup to trigger on that class.

<script>
  
  var btn = document.getElementById("lp-pom-button-6566");
btn.classList.add("lightbox");
  var btn = document.getElementById("lp-pom-button-6497");
btn.classList.add("lightbox");
 </script> 

How can I combine the button id script above with assigning class. I have tried using both scripts on a page, but it won’t add the dynamic parameter in your script.

Please help.

Userlevel 6

Hi @Finge!!

The best option will probably be to assign the class to the buttons before the button id script as opposed to combining them. You could add a line to the button id script to assign the class using jQuery. However that would only apply the class to the button after it’s been clicked, which won’t be effective for using it as a way to launch a popup.

You can though just apply the class above the click function in the button id script. For example

var btn = document.getElementById("lp-pom-button-6566");
btn.classList.add("lightbox");  
  
$("#lp-pom-button-6566").click(function() {
  var url = $(this).attr('href').split("?")[0];
  $(this).attr("href", url + params + character + "plan=Bronze");
});

The above code would apply the “lightbox” class on page load, and then the URL params to track the button click that tiggers the form.

Hope this helps! Let me know if I missed anything with what you’re trying to accomplish.

Userlevel 6
Badge +4

Thank you @Rob 🙂

Adding the class to the script worked, but the parameter “plan” does not pass to the popup. It seems to only work for the built in lightbox form on pages, not when triggering a popup.

Any thoughts on this?

Userlevel 6
Badge +4

@Rob, any chance of getting this script to work for popups too?

Userlevel 6

Hey @Finge I’ve been playing around with this today trying to get it to work with a popup. The problem I’m running into is that the popup will pull in the URL parameters from the referrer in the request that’s made to trigger it. So unless the URL parameter is already present when the page initially loads it doesn’t get passed through to the popup.

So with the example LP, the URL param only get’s passed to the popup if the page initially loads with http://unbouncepages.com/lightbox-button-pre-fill/?plan=gold the “plan=gold” part does get passed. This obviously isn’t helpful. The best thing I could think of would be to have the page reload with the new parameter and then launch the popup “on page load” but only target the specific URL param.

I’ll try and think of some better solutions but can’t promise I’ll be able to come up with anything better (will try my best though!).

Userlevel 6
Badge +4

Thank you for your reply! That’s what I discovered as well. Based on that I did some research and found a script that will append a parameter to the URL without reload. Added bonus - this way I can even add more parameters that will pass to the popup based on what page it loads from.

I am sure there is something that can be done to refine this script so that it tracks if you don’t have incoming parameters and “page variant” will be the first parameter, it will replace “&page_variant” with “?page_variant”.

<script>
  
  var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search + '&page_variant=F&product_interest=ProductX';
window.history.pushState({ path: newurl }, '', newurl);
  
</script>
Userlevel 6

Sorry for the late reply here @danmartin42! I just tried the page and it looks like it’s unpublished. If you’re still struggling with the script let me know and I can take a look for you if you publish the page again or drop a link to the preview screen.

Userlevel 1

Hi @Rob!
Is there a simple method of changing the color of a button using this type of script?
Background: I am working for a charity that does more or less “sporty” competitions. There are two teams (pink and mint) and I am working on a landing page where visitors are asked to become a member of either of those teams.
I want a Lightbox to appear and the “submit” button should be either pink or mint - depending on the choice.

On a related topic: would it be somehow possible to have specific images displayed on the Lightbox, depending on what button was clicked before?

Thanks in advance!

Badge

I’m using this script on a project I’m working on, and it works as expected. Thank you.

I was wondering if it were possible to amend this script to pass additional variables that populate the form, but aren’t displayed using DTR.

For example, if I wanted to populate another variable called “conv_type_processing” with “Channel Partner Lead - International Channel Campaign 2021 PAM Level 1” how would I do that?

Thanks so much.

Badge

I’ve worked it out! Whoop. I went out for a walk and was working through the script in my head and figured out that all I need to do is separate the variables passed with a “&”.

Thanks to the original author.

Reply