-
AuthorPosts
-
October 14, 2015 at 5:42 am #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;
BitSoulOctober 14, 2015 at 5:51 pm #518863Hi 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.
October 14, 2015 at 11:20 pm #518974Hi 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
October 15, 2015 at 12:48 pm #519228Hi,
Glad you got it fixed, please let us know if you should need any more help on the topic.
Thanks,
RikardNovember 19, 2015 at 2:08 pm #538974Hey 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 addonsubmit
attribute to the form it will catch correct information.November 21, 2015 at 2:10 am #540029Hi!
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!
JosueNovember 21, 2015 at 2:36 pm #540162Hey Josue!
Will it work after next update?
November 23, 2015 at 11:59 am #540657No, unless you do something like this:
http://kriesi.at/documentation/enfold/add-new-or-replace-advanced-layout-builder-elements-from-child-theme/Cheers!
JosueNovember 25, 2015 at 5:23 am #542116Hi 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;
MarkNovember 25, 2015 at 10:11 am #542233Hi Mark!
Can you expand on what you’re trying to do?
Best regards,
JosueNovember 25, 2015 at 6:59 pm #542640Hello 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
November 30, 2015 at 7:56 pm #544766Hey!
@bitsoul
please do create a new task thread so we can keep solutions cleans!Thanks a lot for your patience
Cheers!
Basilis -
AuthorPosts
- The topic ‘GA Tracking Code Individual Forms’ is closed to new replies.