Link tracking in Membership plugin

Does anyone know how to track clicks on protected content links? I have a PDF that is protected content in the membership plug, and the link is obfuscated using the hybrid setting (path and filename are changed to prevent illegal downloads). Only subscribers can see the links. I want to know how many times the file was downloaded, and ideally by whom.

I can use Google Analytics to see how many times a link is clicked, but that will not tell me who that was, just their IP address.

Any ideas?

  • Michael Bissett

    Hey @steve199, hope you're doing well today! :slight_smile:

    Not sure what type of additional details you're looking to obtain here, but unless the user in question was a logged in user (which may require some custom development to implement, not seeing that there's a solution that would track this sort of activity), it would seem that you'd need to get the additional details you seek by running the IP address through one of these tools:

    http://whatismyipaddress.com/ip-lookup
    http://www.iplocation.net/

    Kind Regards,
    Michael

  • steve199

    I thought this may help someone with the same issue I had. Again, the problem I am solving here is that I have a single PDF file on my site, and it is only downloadable by people who are paying for a subscription through the membership plugin. I want to know how many times the PDF link is clicked, and who it is that clicked on it. Both pieces of data are critical, as we want to know how popular each PDF is (total clicks) and who downloaded the PDF (so we can use this data to market a renewal to).

    Since this isn't baked into the membership plugin, I came up with a solution for this to record the total downloads and who clicked on it in the posts meta data table. This data I can pull into a custom report, the code of which I'll post later. The key here is that only one PDF per post can be tracked, but for me that is not a problem as I only have one downloadable PDF file per post.

    Each link I have has the following syntax:

    Download Article

    The key here is the id of the PDF. That is what will be hooked to the Ajax call below.

    The code that I put in my themes functions.php file (although you could make a separate plugin of this) is as follows:

    //Link Clicks Counter
    add_action( 'wp_ajax_link_click_counter', 'link_click_counter' );
    
    function link_click_counter() {
        if ( isset( $_POST['nonce'] ) &&  isset( $_POST['post_id'] ) && wp_verify_nonce( $_POST['nonce'], 'link_click_counter_' . $_POST['post_id'] ) ) {
            $count = get_post_meta( $_POST['post_id'], 'link_click_counter', true );
            update_post_meta( $_POST['post_id'], 'link_click_counter', ( $count === '' ? 1 : $count + 1 ) );
            add_post_meta( $_POST['post_id'], 'who_downloaded_pdf', get_current_user_id());
        }
            //echo $count; //uncomment for testing
    	wp_die();
    }
    
    add_action( 'wp_footer', 'link_click_head' );
    
    function link_click_head() {
        global $post;
        if( isset( $post->ID ) ) {
    ?>
        <script type="text/javascript" >
    
            jQuery(function ($) {
                $( '#countable_link' ).on( 'click', function() {
    
                    var self = $( this );
                    jQuery(document).ready(function($) {
                        ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
                		var data = {
                			'action': 'link_click_counter',
                            'nonce': '<?php echo wp_create_nonce( 'link_click_counter_' . $post->ID ); ?>',
                            'post_id': '<?php echo $post->ID; ?>'
                		};
    
                		$.post(ajaxurl, data, function(response) {
                			//alert('Got this from the server: ' + response);   //Uncomment for testing.
                		});
             		});
               });
            });
    
        </script>
    <?php
        }
    }
    ?>

    The net result is that I have one record in the meta table for the post id of xxx that has a meta key of "link_click_counter" with the total number of clicks. I also have individual records in the meta table for the post id of xxx that has a meta key of "who_downloaded_pdf" with the User ID of the logged in user that clicked on the link.

    This makes it really simple to build a custom report (which I'll post on this thread at a later time) that shows the total clicks on the PDF and the users who downloaded the PDF. It doesn't show the actual date that they downloaded it, and for my purposes I don't really care about that as I'm going to use Google Analytics to track the usage pattern.

    Hope this helps someone.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.