Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Last active March 13, 2023 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marklchaves/27d20fc27d7bb362c179bf58c0c3ebb7 to your computer and use it in GitHub Desktop.
Save marklchaves/27d20fc27d7bb362c179bf58c0c3ebb7 to your computer and use it in GitHub Desktop.
Bypass the default GF view counter logic so we count views for forms inside a popup only when the popup shows the form
<?php // Ignore this first line when copying to your child theme's functions.php file.
/**
* According to Gravity Forms Support on 9 March 2023, "The views column records the number of times the
* form markup is generated ... including when the form is redisplayed following validation errors.
* It's not strictly how many times the form was actually viewed."
*
* That means the Gravity Forms form view counter gets bumped up even if the form is hidden in a tab, popup, slider, and
* accordion (to name a few). Bottom line: don't trust the form view counter as a valid impression stat.
*/
/**
* Luckily Popup Maker has 2 solutions:
*
* 1) Use the Remote Content (RC) extension. RC popups are optimized to only load popup content when a popup
* launches (becomes visible).
* 2) Hook into Popup Maker Analytics. Popup Maker Analytics only counts popup impressions when a popup displays
* (as it should). You'll need to disable the view counter first, then manually increment it when the analytics
* code runs. See the code below to learn how to do that.
*/
// This disables the view counter for all forms. Read the instructions below
// if you must disable the counter selectively for certain forms.
add_filter( 'gform_disable_view_counter', '__return_true' );
/*
* If there are other GF forms on your site that aren't in popups, you
* must use the form ID version of the filter above.
*
* E.g.: add_filter( 'gform_disable_view_counter_5', '__return_true' );
*
* Reference: https://docs.gravityforms.com/gform_disable_view_counter/
*
* WARNING: If you have a form on a page and use the same form in a popup,
* you'll only see view counts for the popup instance. Or, vice versa.
* Just avoid doing this. Make a separate copy of the form so you can
* use 1 copy on a page and the other copy in the popup if needed.
*/
/*
* Hook into Popup Maker Analytics so when a popup that has a Gravity Forms (GF)
* form in it opens, increment the GF form view counter.
*/
add_action( 'pum_analytics_open', function( $args ) {
$form_id;
$popup = pum_get_popup( $args ); // Grab the popup that's passed in.
$shortcode_name = 'gravityform'; // The shortcode to search for.
$content = $popup->post_content; // Grab the popup content.
$pattern = get_shortcode_regex(array($shortcode_name));
if (preg_match_all('/'.$pattern.'/s', $content, $matches)
&& array_key_exists(2, $matches)
&& in_array($shortcode_name, $matches[2])) {
$shortcode = $matches[0][array_search($shortcode_name, $matches[2])];
// If we found a shortcode, grab the ID argument from it.
// Assumes the ID is the first arg and there's only 1 space
// between the shortcode name and the id arg.
// E.g., [gravityform id="2" ... ]
// Feel free to refactor.
$regex = '/\[' . $shortcode_name . ' id="([^"]+)"/';
if (preg_match($regex, $shortcode, $matches)) {
$form_id = $matches[1];
RGFormsModel::insert_form_view( $form_id ); // TO DO: Maybe test for a valid ID before calling this.
} // if
} // if
} );
@marklchaves
Copy link
Author

Reply from Gravity Forms Support Team

Gravity Forms has no plans to stop counting invalid views (i.e., incrementing the view counter when it should not).

popup-maker-gravity-forms-view-counter-reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment