Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #518489

    Hi All,

    I’d like to apply your code as seen here – http://kriesi.at/documentation/enfold/add-onclick-event-to-the-contact-form-submit-button/

    But, I’m having trouble targeting separate forms.

    I can target all forms, but not separate, individual forms.

    Your quote: ” These variables are useful if you want to set different attributes for different contact forms.”

    Would you please provide an example.

    Thank you;
    BitSoul

    #518863

    Hi bitsoul!

    Use this.

    add_filter('avf_contact_form_submit_button_attr','avia_add_submit_attributes_to_cf', 10, 3);
    function avia_add_submit_attributes_to_cf($att, $formID, $form_params){
      echo '<pre>';
      echo var_dump($formID);
      echo '</pre>';
      $att = "onclick=\"_gaq.push(['_trackPageview', '/VP/XXX/XXX']);\"";
      return $att;
    }

    And then view your page with the contact form and write down all of the IDs for each form you want to target and then when you have them all switch the code to this.

    add_filter('avf_contact_form_submit_button_attr','avia_add_submit_attributes_to_cf', 10, 3);
    function avia_add_submit_attributes_to_cf($att, $formID, $form_params){
      if ( $formID == 5 ) { $att = "onclick=\"_gaq.push(['_trackPageview', '/VP/XXX/XXX']);\""; }
      if ( $formID == 7 ) { $att = "onclick=\"_gaq.push(['_trackPageview', '/VP/XXX/XXX']);\""; }
      return $att;
    }

    Cheers!
    Elliott

    • This reply was modified 9 years, 2 months ago by Elliott.
    #518974

    Hi Elliot,

    Thank you.

    That was the kick-in-the-pants I needed..!

    I find that the $formID is NOT unique to individual forms site-wide – but rather to a form(s) on any one page.

    In my case, it’s better I use UNIQUE text inside the “submit” button – values which can be found in the $form_params array.

    _gaq.push is old school now – traditional ga.js code.

    ga send is today – advanced analytics.js code

    My solution:

    /* To identify $form_parameters AND $formID
     * Insert into child theme functions.php
     * Running this code will "break" your install for visitors - so it's better to do it in a staging environment
     * Comment out code when done
    */
    /*
    add_filter( 'avf_contact_form_submit_button_attr','avia_add_submit_attributes_to_cf', 10, 3 );
    
    function avia_add_submit_attributes_to_cf( $att, $formID, $form_params )
    {
      echo '<pre>';
      echo var_dump( $formID ); //get the ID
      echo '</pre>';
      echo '<pre>';
      echo var_dump( $form_params ); //get all the parameters
      echo '</pre>';
    }
    */
    /* Add onclick event to contact form submit button
     * Insert into child theme functions.php
     * You only need to declare a "unique submit button string" once for it to be used site-wide
     * http://kriesi.at/documentation/enfold/add-onclick-event-to-the-contact-form-submit-button/
     * For Google Analytics goal tracking - advanced analytics.js
     * 10/14/2015
     */
    
    add_filter( 'avf_contact_form_submit_button_attr', 'avia_add_submit_attributes_to_cf', 10, 3 );
    
    function avia_add_submit_attributes_to_cf( $att, $formID, $form_params ) {
    
      //Form on any and all pages
    
      if(in_array( "unique submit button string 1", $form_params, TRUE ))
      {
        $att = "onClick=\"ga( 'send', 'event', { eventCategory: 'xxx', eventAction: 'xxx', eventLabel: 'xxx', eventValue: xx } );\"";
        return $att;
      }
    
      //Special form on page-2 - but I can use it anywhere if I want
    
      if(in_array( "unique submit button string 2", $form_params, TRUE ))
      {
        $att = "onClick=\"ga( 'send', 'event', { eventCategory: 'yyy', eventAction: 'yyy', eventLabel: 'yyy', eventValue: yy } );\"";
        return $att;
      }
    
      //Another additional special form on page-2 - hey... same for me
    
      if(in_array( "unique submit button string 3", $form_params, TRUE ))
      {
        $att = "onClick=\"ga( 'send', 'event', { eventCategory: 'zzz', eventAction: 'zzz', eventLabel: 'zzz', eventValue: zz } );\"";
        return $att;
      }
    
      //Add as many as you need..!
    }
    • This reply was modified 9 years, 2 months ago by bitsoul. Reason: Formatting
    #519228

    Hi,

    Glad you got it fixed, please let us know if you should need any more help on the topic.

    Thanks,
    Rikard

    #538974

    Hey guys!

    Is it possible to add onsubmit atrribute to the form?
    If I press on empty button, GA will catch it, but data is wrong cause I haven’t send any information to the server.
    But if we add onsubmit attribute to the form it will catch correct information.

    #540029

    Hi!

    Replace line 142-143 in /framework/php/class-form-generator.php with:

    			$form_attr = apply_filters('avf_contact_form_attr', '', $this->formID, $this->form_params);
    			$this->output  = '<form action="'.$params['action'].'" method="post" '.$form_data.' class="'.$form_class.' '.$extraClass.'" data-avia-form-id="'.$this->formID.'" '.$redirect.' '.$form_attr.'><fieldset>';
    

    That will enable you a avf_contact_form_attr filter you can use.

    Cheers!
    Josue

    #540162

    Hey Josue!

    Will it work after next update?

    #540657
    #542116

    Hi All,

    I like your work.

    I’d like to be able to target separate, individual forms with on submit.

    Say, with the Contact Forms’s Custom CSS Class.

    Can you help me out?

    Best;
    Mark

    #542233

    Hi Mark!

    Can you expand on what you’re trying to do?

    Best regards,
    Josue

    #542640

    Hello Josue,

    I have multiple contact forms for various purposes.
    I use Google Analytics for reporting form submits.
    I may have multiple forms with different purposes and different GA scripts on any one page.
    I may use the same GA tracking code on any form on any different page.

    I can target ALL forms and insert GA script using your code suggestions for “onsubmit” – but I’d like to be able to target separate, individual forms and insert different GA scripts..

    Using the GA “onClick” script you helped with I’m able to track my forms by targeting unique strings used inside the buttons —
    if(in_array( “unique submit button string”, $form_params, TRUE ))

    But, I do sometimes get a false positive because of an errant click. This is why I like the idea of using an “onsubmit” event.

    I know I can use multiple “Thank You” pages as an error proof means of tracking form submittals – but that seems cumbersome.

    I see that your shortcode script appends the Contact Form’s Custom CSS Class to the end of “form class=” in it’s HTML output.

    If I could target that unique Custom CSS Class string in an “if” statement similar to how I’ve done it with my “onClick” script – that would be awesome.

    Sadly, I’ve not been successful.

    Thank you all for an amazing theme and the best support!

    Mark

    #544766

    Hey!


    @bitsoul

    please do create a new task thread so we can keep solutions cleans!

    Thanks a lot for your patience

    Cheers!
    Basilis

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘GA Tracking Code Individual Forms’ is closed to new replies.