Tagged: CONTACT FORM, GTM, submission, tag manager, Tracking
-
AuthorPosts
-
January 25, 2019 at 1:12 pm #1058694
Hi,
first of all, this was my premier enfold experience, and this theme in combination with WP5 on PHP 7.2 was the most hastle-free, intuitive, quick responding, best documented WP project I have ever worked on. Multiple thumbs up.
To the meat: I want a form submission event pushed to datalayer when the contact form adds its “avia-form-success” heading element to the page, and not earlier, because the button click can occur multiple times without successful submission.
Basically this “dataLayer.push({‘event’: ‘contactReuquestSubmitted’});” on the ajax response. How could I wire it into the template?
Thanks and bw
PJanuary 27, 2019 at 9:53 pm #1059426Hey delphostkp,
Sorry for the late reply, is the dataLayer.push in regards to the Google Tag Manager?
I was able to put together a jQuery script to fire a trigger when the class “avia-form-success” is added the div “ajaxresponse”$(function() { var button = $('.button') , response = $('.ajaxresponse') ; button.on('click', function() { response.addClass('avia-form-success'); $(document).trigger('ajaxAlert'); }); $(document).on('ajaxAlert', function() { alert("The response has been given"); }); });
this example gives an alert box popup, but you could replace the “alert” with your “dataLayer.push”
This can be added to the functions.php by wrapping it in a WP function like this:function custom_script(){ ?> <script> (function($){ //your jQuery here })(jQuery); </script> <?php } add_action('wp_footer', 'custom_script');
Best regards,
MikeJanuary 28, 2019 at 6:20 pm #1059741Hi Mike,
thanks for your help. Yes it’s for GTM. However, with your solution, the click produces the trigger without respecting form validation. What’s needed is the datalayer push only after the success-heading becomes visible.
I found a way to output the data-layer push with form validation in config-templatebuilder/avia-shortcodes/contact/contact.js.
responseContainer.removeClass('hidden').css({display:"block"}); window.dataLayer = window.dataLayer || []; window.dataLayer.push({'event': 'contactFormSuccess'}); form.slideUp(400, function(){responseContainer.slideDown(400, function(){ $('body').trigger('av_resize_finished'); }); send.formElements.val('');});
But how would I overwrite the contact.js in a child theme?
BW
January 29, 2019 at 3:56 am #1059876Hi,
To overwrite to contact element, add a directory /shortcodes/ to your child theme,
then copy the /contact/ directory to it with all 3 files in it, contact.js, contact.css, contact.php
so you will have this path: /enfold-child/shortcodes/contact/
Then add this code to the end of your functions.php file in Appearance > Editor:add_filter('avia_load_shortcodes', 'avia_include_shortcode_template', 15, 1); function avia_include_shortcode_template($paths) { $template_url = get_stylesheet_directory(); array_unshift($paths, $template_url.'/shortcodes/'); return $paths; }
now your child theme element will overwrite the theme element, you can read more here.
Best regards,
MikeJanuary 29, 2019 at 10:17 am #1059925Hi Mike, it works for the contact.php, but not for the contact.js. Anything else that needs to be done for the contact.js to be read from the child theme? BW
January 29, 2019 at 2:51 pm #1060011Hi,
I took a look at contact.php and found on lines 36-39 it is calling the theme css & js://load css wp_enqueue_style( 'avia-module-contact' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/contact/contact.css' , array('avia-layout'), false ); //load js wp_enqueue_script( 'avia-module-contact' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/contact/contact.js' , array('avia-shortcodes'), false, TRUE );
Try changing the path like this:
//load css wp_enqueue_style( 'avia-module-contact' , $path['pluginUrl'].'/wp-content/themes/enfold-child/shortcodes/contact/contact.css' , array('avia-layout'), false ); //load js wp_enqueue_script( 'avia-module-contact' , $path['pluginUrl'].'/wp-content/themes/enfold-child/shortcodes/contact/contact.js' , array('avia-shortcodes'), false, TRUE );
that is assuming that your child theme directory is /enfold-child/ like mine is.
Best regards,
MikeFebruary 6, 2019 at 10:56 am #1063548Hi Mike, thx for your help and sorry for the late response. Your solution works. I know this might be outside your scope, but do you have a quick idea why it works on my local dev but not on my live system? On the live system I get the following error thrown, when loading the scripts via child theme:
Notice: Undefined variable: path in [SERVER PATH]/wp-content/themes/enfold-child/shortcodes/contact/contact.php on line 36 Notice: Undefined variable: path in [SERVER PATH]/wp-content/themes/enfold-child/shortcodes/contact/contact.php on line 39 Warning: session_start(): Cannot start session when headers already sent in [SERVER PATH]/wp-content/themes/enfold-child/shortcodes/portfolio.php on line 34 Warning: session_start(): Cannot start session when headers already sent in [SERVER PATH]/wp-content/themes/enfold/config-templatebuilder/avia-shortcodes/masonry_entries/masonry_entries.php on line 56
February 6, 2019 at 2:21 pm #1063630Hi,
It sounds like changing the path from AviaBuilder::$path[‘pluginUrlRoot’] to $path[‘pluginUrl’] is not working correctly on you live site. But as you note it is working on the localhost.
A similar issue came up last week and the solution was to change the path to get_template_directory()
I’m not sure why a localhost path would be different than a live host, but if you include FTP access & admin login I will take a look.
I guess worse case we could change the path to a absolute path.
** I did a quick search and found some tips on using dirname(__FILE__) instead, but it would have to be tested first.Best regards,
MikeFebruary 6, 2019 at 5:01 pm #1063702Hi again,
I fiddled around with the above hints, but couldn’t get it to work. However, your links got me to another thread here, I exchanged the script names, put the contact module js script in /enfold-child/js/ and now it works.
New function to exchange the script reference in functions.php:
function wp_change_aviacontactjs() { wp_dequeue_script( 'avia-module-contact' ); wp_enqueue_script( 'avia-module-contact-child', get_stylesheet_directory_uri().'/js/contact.js', array('jquery'), 2, true ); } add_action( 'wp_print_scripts', 'wp_change_aviacontactjs', 100 );
Thanks!
February 7, 2019 at 5:30 am #1063818Hi,
Glad to hear. Thanks for sharing your solution.
I assume we can close this now, but I like to ask. Shall we close this then?Best regards,
MikeFebruary 7, 2019 at 11:02 am #1064012Yes, this can be closed. Thanks Mike
February 7, 2019 at 2:54 pm #1064149Hi,
Glad we were able to help, we will close this now. Thank you for using Enfold.For your information, you can take a look at Enfold documentation here
For any other questions or issues, feel free to start new threads under Enfold sub forum and we will gladly try to help you :)Best regards,
Mike -
AuthorPosts
- The topic ‘Pushing submission datalayer event with form ajax response’ is closed to new replies.