PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Scalability and gform_pre_submission

  1. Hello,

    I'm attempting to catch the cookie data from google-analytics utm.gif and pass those values to gravity forms which has the sales force add-on. Here is my code:

    I have several inquiries about the performance. This code does catch the cookie, and pass the value. In this case $_POST[ 'input_3' ] is the hidden field we are giving the value for one of the GA parameters.

    I need to replicate this same functionality for about a dozen forms, not just form with the id of 39. I need to create hidden inputs for each of the forms and i'm assuming they will all have a unique name attribute and therefore seems like I will need to create a custom function for each one to give value to. Is there a better way about programming this so that its more automated and does not require me to edit the script if hidden fields are added/removed or new forms are created?

    I am trying to streamline this as best as possible for a client

    class GA_Tracking {
    
        function __construct( ) {
    
          add_action( 'gform_pre_submission_39', array( &$this, 'form_39' ) );
    
        }
    
        function cookie( ) {
    
          if( $cookie = $_COOKIE[ '__utmz' ] ) {
    
            function strnpos( $haystack, $needle, $occurance, $pos = 0 ) {
    
                for ( $i = 1; $i <= $occurance; $i++ ) {
    
                    $pos = strpos( $haystack, $needle, $pos ) + 1;
    
                }
    
                return $pos;
    
            }
    
            $paramsstr = substr( $cookie, strnpos( $cookie, '.', 4 ) );
    
            $params = explode( '|', $paramsstr );
    
            $param = array( );
    
            foreach( $params as $key => $value ) {
    
              $split = explode( '=', $value );
    
              $param[ $split[ 0 ] ] = $split[ 1 ];
    
            }
    
          }
    
          return $param;
    
        }
    
        function form_39( ) {
    
          $param = $this->cookie( );
    
          $_POST[ 'input_3' ] = $param[ 'utmcsr' ];
    
        }
    
      }
    
      $gat = new GA_Tracking;
    Posted 12 years ago on Friday August 3, 2012 | Permalink
  2. Instead of gform_pre_submission_39 you can use gform_pre_submission, then in your function you can check the form ID and if the ID of the current form is in the array you define, you can process your code. That somewhat automates it, but you still need to add the ID of new forms to the array in functions.php. That's not ideal.

    What you need is a way to check if the form being processed has a specific field, or marker, or identification, and then run your code. One way to do that is to loop through all the fields in a form, and look for a custom CSS class. You can find out how to do it on this page, although it's being used for custom validation, not to hook your code to.

    http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook

    The important parts: this will loop through each field in a form (this maybe be inefficient in different ways, since every form and every field in the form will need to be processed. There's a scalability problem of a different sort for you.)

    foreach($form['fields'] as &$field){
    // If the field does not have our designated CSS class, skip it
    if(strpos($field['cssClass'], 'run-ga-code') === false)
        continue;

    That would skip any field that does not contain the custom CSS class of "run-ga-code". In the event of a match, proceed with your function.

    You've already accomplished quite a bit with the code you've written. Hopefully, this information will spark you in a direction to make this more efficient for you and your client.

    Posted 12 years ago on Monday August 6, 2012 | Permalink
  3. Thanks Chris for some pointers, I definitely took a look into it but i'm still not sure that it will work out for me because the hidden fields that I add all have different input names. For example the form with the ID 39 may have the hidden field as input_3, whereas a separate form has the same hidden field but with a different name such as input_5.

    Hidden fields also do not seem to support a custom CSS class but even if it did, I dont see how I could set the value of the hidden field by just knowing the class name or inputName, which I do

    It seems like I will have to check each form and each input to set the values and there really doesnt seem to be a better solution

    function form_39( $form ) {
    
      $param = $this->cookie( );
    
      if( $param[ 'utmcsr' ] ) {
      $_POST[ 'input_3' ] = $param[ 'utmcsr' ];
      }
      if( $param[ 'utmccn' ] ) {
      $_POST[ 'input_2' ] = $param[ 'utmccn' ];
      }
      if( $param[ 'utmcmd' ] ) {
      $_POST[ 'input_4' ] = $param[ 'utmcmd' ];
      }
      if( $param[ 'utmcct' ] ) {
      $_POST[ 'input_5' ] = $param[ 'utmcct' ];
      }
      if( $param[ 'utmctr' ] ) {
      $_POST[ 'input_6' ] = $param[ 'utmctr' ];
      }
    
    }
    Posted 12 years ago on Tuesday August 7, 2012 | Permalink
  4. You're right about the hidden fields not accepting a CSS Class name. There wouldn't be any point. You could use regular fields and then hide them in the CSS. To avoid having to add unique names to the stylesheet (which would defeat the purpose) you could use the same custom class name and mark that in the stylesheet as display:none. That would effectively make it hidden to the viewer and would allow you to add a CSS class to it at the same time, then use the aforementioned process to loop through the fields.

    I'm sure with more thought we can come up with a solution to ease the management of this somewhat.

    Posted 12 years ago on Tuesday August 7, 2012 | Permalink