Count and show Clicks on external links in a single page/post

Yes, it is possible. You can do this with ajax call that updates post meta field before the link is followed.

In the example, I used admin and non-admin users who click the link and automatic increase link_check_click_counter in the post meta. I used here the example to show the data using wp_footer. You can also use wp_head instead of wp_footer. Copy and paste the code and add it to functions.php. When you clicked on link link_check_click_counter meta will create for that post and you can track how many clicks on the link.

HTML

<div id="link_count">
  <a href="https://www.dropbox.com/">Dropbox</a>
  <a href="https://www.mediafire.com/">Mediafire</a>
  <a href="http://google.com">google.com</a>
  <a href="http://www.linkedin.com/in/turjo">Linkadin</a>
</div>

PHP

<?php
/* functions.php */
add_action( 'wp_ajax_link_check_click_counter', 'link_check_click_counter');
add_action( 'wp_ajax_nopriv_link_check_click_counter', 'link_check_click_counter' );
function link_check_click_counter() {

    if ( isset( $_POST['nonce'] ) &&  isset( $_POST['post_id'] ) && wp_verify_nonce( $_POST['nonce'], 'link_check_click_counter_' . $_POST['post_id'] ) ) {
        $count = get_post_meta( $_POST['post_id'], 'link_check_click_counter', true );
        update_post_meta( $_POST['post_id'], 'link_check_click_counter', ( $count === '' ? 1 : $count + 1 ) );
    }
    exit();
}


add_action( 'wp_footer', 'link_click' );
//add_action( 'wp_head', 'link_click' );
function link_click() {
    global $post;

    if( isset( $post->ID ) ) {
?>
    <script type="text/javascript" >
    jQuery(function ($) {
        var ajax_options = {
            action: 'link_check_click_counter',
            nonce: '<?php echo wp_create_nonce( 'link_check_click_counter_' . $post->ID ); ?>',
            ajaxurl: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
            post_id: '<?php echo $post->ID; ?>'
        };

        $( '#link_count a' ).on( 'click', function() {
            var href = $( this ).attr( "href" );
            var redirectWindow = window.open(href, '_blank');   
            $.post( ajax_options.ajaxurl, ajax_options, function() {
                redirectWindow.location;
            });
            return false;
        });
    });
    </script>
<?php
    }
}
?>

Link Count Of a Post

global $post;
print get_post_meta($post->ID,'link_check_click_counter',true);

Sum all the counts from all the posts

   $all_link_count = link_check_meta_values( 'link_check_click_counter', 'page' );
    $total = array_sum($all_link_count);
    print $total;

//add this in functions.php

function link_check_meta_values( $key = '', $type="post", $status="publish" ) {

    global $wpdb;

    if( empty( $key ) )
        return;

    $r = $wpdb->get_col( $wpdb->prepare( "
        SELECT pm.meta_value FROM {$wpdb->postmeta} pm
        LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
        WHERE pm.meta_key = '%s'
        AND p.post_status="%s"
        AND p.post_type="%s"
    ", $key, $status, $type ) );

    return $r;
}

Leave a Comment