How to name a Facebook event inside js hook gform_page_loaded?

We want to log inside of FB Event Manager which page/question users are exiting. We decided to dynamically name the event.

We copied the JS hook code example from the GF dev docs to allow us to run our code on each page/question loaded. Inside that function we use the Facebook conversion tracking event code block per their dev docs. However, when we try to dynamically name the event, facebook doesn’t recognize the event name.

When I name the FB event with a static string (ex = “event1”), it works as expected. However, this is not dynamically naming the events the page number. I used the current_page.toString() method, but Facebook Event Manger still doesn’t recognize the event name.

Our form uses AJAX with 1 question on each page, 10 questions total. I feel this is a coding issue on my side - not Facebook’s nor Gravity issue. Why won’t the facebook block of code recognize the parameter?

Code that doesn’t recognize the event name (suppose to be current_page)
<script type="text/javascript"> jQuery(document).on('gform_page_loaded', function(event, form_id, current_page){
fbq(“trackCustom”, current_page, { futureParameter: “Future_Use” });
}); </script>

Code that Facebook recognizes the event name I define
<script type="text/javascript"> jQuery(document).on('gform_page_loaded', function(event, form_id, current_page){
fbq(“trackCustom”, "stringOfCharacters", { futureParameter: “Future_Use” });
}); </script>

Thanks for taking a look at this code challenge I have been stuck on for a couple hours.

The below snippet works to dynamically name custom events using current_page in Facebook. Change ‘MyEventName-’ with your own. Current Page returns a number and these will be concatenated in a string.

jQuery(document).on(
  'gform_page_loaded',
  function (event, form_id, current_page) {
    return fbq('trackCustom', 'MyEventName-' + current_page, {
      futureUse: 'to_be_defined_later',
    });
  }
);

A StackOverflow user believes Facebook has a underwritten rules on how event names can be titled. The current_page returns a number and FB may not like that, even if the number is a string data type.

Thanks for the update Carlos.

1 Like