Google Tracking Telephone Clicks w/ WordPress integration

, , ,

So if you have jQuery and the gtag method for Google Analytics running on your site you can add the following code to the head tag of your site.

jQuery( document ).ready( function() {
    jQuery("a[href^='tel']").on("click",function(){
        gtag( 'event', 'click_to_call', {
            'event_category': 'Click Tracking',
            'event_label': 'Phone Number Click',
        });
    });
});

One way that I like implementing it is creating a stand along JS file that is located in the Child Theme of the site.

jQuery( document ).ready( 
   function() { 
      jQuery("a[href^='tel']").on("click",function(){ 
         gtag( 'event', 'click_to_call', { 'event_category': 'Click Tracking', 'event_label': 'Phone Number Click', }); 
   }); 
});

And then in the functions file I would include the following to allow it to ensure jQuery was getting included in the frontend of the site.

if (!function_exists('enqueue_child_theme_scripts')) {
    function enqueue_child_theme_scripts() {
        wp_enqueue_script( 'google-tel', get_stylesheet_directory_uri() . '/google-tel.js', array('jquery'), filemtime( get_stylesheet_directory() . '/google-tel.js') , true );

    }
    add_action('wp_enqueue_scripts', 'enqueue_child_theme_scripts');
}

 

Skills

Posted on

November 26, 2019

Submit a Comment

Your email address will not be published.