• Resolved todeswalzer

    (@todeswalzer)


    Hello, first of all thank you for your work.

    I hope you wouldn’t mind to help me a bit. I have a list of popular posts on the front page, and i’m trying to make it to not count if a user comes to a post from the front page.
    The problem is next:

    
    add_action( 'wpp_pre_update_views', 'wpp_frontpage_view', 10, 2 );                                                                                                                       
    function wpp_frontpage_view( $post_ID, $views ) {
    
        // I want to add here a check if a
        // user comes from the front page
        // but wp_referer() always returns
        // url to the current post
        $referer = wp_referer(); // current post url
    
    }
    
    • This topic was modified 8 years, 1 month ago by todeswalzer.
    • This topic was modified 8 years, 1 month ago by todeswalzer.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @todeswalzer,

    I’m seeing the same behavior as you are. No idea what’s going on, to be honest.

    Here’s a different workaround:

    /**
     * Appends a special parameter to the URL to
     * prevent updating the views count of an article
     * if referred from home.
     * 
     */
    function my_custom_single_popular_post( $post_html, $p, $instance ){
    
        // Return popular post link with "do not track this" parameter
        if ( is_home() ) { // might need to change this condition with is_front_page()
            $output = '<li><a href="' . get_permalink($p->id) . ( is_home() ? "?track=0" : "" ) . '" class="my-custom-title-class" title="' . esc_attr($p->title) . '">' . $p->title . '</a></li>';
        } // Return the regular popular post link
        else {
            $output = '<li><a href="' . get_permalink($p->id) . '" class="my-custom-title-class" title="' . esc_attr($p->title) . '">' . $p->title . '</a></li>';
        }
    
        return $output;
    
    }
    add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
    
    /**
     * Exclude post type 'post' from WPP's tracking 
     * on demand.
     */
    function my_trackable_post_types( $post_types ){
    
        if ( isset( $_GET['track'] ) && 0 == $_GET['track'] ) {
            $exclude_from_tracking = array( 'post' );
            $post_types = array_diff( $post_types, $exclude_from_tracking );
        }
    
        print_r( $post_types );
    
        return $post_types;
    
    }
    add_filter( 'wpp_trackable_post_types', 'my_trackable_post_types', 10, 1 );

    Explanation:

    We’re going to use two hooks: wpp_post and wpp_trackable_post_types. The former allows me to customize the HTML output of every popular posts, while the latter allows me to tell WPP which post types it should track.

    In my_custom_single_popular_post() we check if the user is currently viewing the home page (check the code for details, you may need to adjust it) and if so then we append an arbitrary parameter to the URL of the post for later use (I chose track for this example but you can name it whatever you want).

    When the user visits the post using this link, my_trackable_post_types() will check if the parameter we set before (track in my example) is present and if it’s 0 (you may want to tweak the conditions). If the conditions are met, it’ll ask WPP not to track this pageview by excluding the post type post from the array of post types WPP tracks by default (post, page and every public custom post type registered by your theme or a plugin, in case you’re wondering).

    So, basically, with this we’re telling WPP:

    “Hey, if the URL has X parameter and the post type is Y, don’t increment the views count.”

    There might be a more elegant way of doing this but I’m pretty tired now (and it’s only Tuesday D:) and this is the best my brain can do at this moment 😛

    Thread Starter todeswalzer

    (@todeswalzer)

    Thank you very much. Btw wp_referer() works as it should in wpp_trackable_post_types filter.

    Plugin Author Hector Cabrera

    (@hcabrera)

    That’s even better, you can do everything with a single hook. Thanks for reporting back!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Don’t count users from the front page’ is closed to new replies.