Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #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
    P

    #1059426

    Hey 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,
    Mike

    #1059741

    Hi 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

    #1059876

    Hi,
    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,
    Mike

    #1059925

    Hi 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

    #1060011

    Hi,
    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,
    Mike

    #1063548

    Hi 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
    #1063630

    Hi,
    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,
    Mike

    #1063702

    Hi 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!

    #1063818

    Hi,
    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,
    Mike

    #1064012

    Yes, this can be closed. Thanks Mike

    #1064149

    Hi,
    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

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Pushing submission datalayer event with form ajax response’ is closed to new replies.