• Resolved sekedus

    (@sekedus)


    How to exclude internal traffic with cookies?

    Example: views are not counted, if the “internal_user” cookie is exists.

    Edit:
    {Feature request}

    Can you add an option to exclude traffic with cookies in WPP Tools?

    Modified code:

    (function(){
        try {
            var wpp_json = document.querySelector("script#wpp-json"),
                do_request = true;
    
            wpp_params = JSON.parse(wpp_json.textContent);
    
            if ( wpp_params.ID ) {
                if ( '1' == wpp_params.sampling_active ) {
                    var num = Math.floor(Math.random() * wpp_params.sampling_rate) + 1;
                    do_request = ( 1 === num );
                }
    	
                if (document.cookie.match(RegExp('(?:^|;\\s*)internal_user=([^;]*)'))) {
                    do_request = false;
                    window.console.log('WPP: Internal traffic');
                }
    
                if ( do_request ) {
                    WordPressPopularPosts.post(
                        wpp_params.ajax_url,
                        "_wpnonce=" + wpp_params.token + "&wpp_id=" + wpp_params.ID + "&sampling=" + wpp_params.sampling_active + "&sampling_rate=" + wpp_params.sampling_rate,
                        function( response ) {
                            wpp_params.debug&&window.console&&window.console.log&&window.console.log(JSON.parse(response));
                        }
                    );
                }
            }
        } catch (err) {
            console.error("WPP: Couldn't read JSON data");
        }
    })();
    • This topic was modified 12 months ago by sekedus.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @sekedus,

    How to exclude internal traffic with cookies?

    Can you add an option to exclude traffic with cookies in WPP Tools?

    Unfortunately I will not add such a feature to the plugin because it’s just too niche. I don’t think it’s something that other people will find useful / beneficial.

    With that being said, you can use PHP and the wpp_trackable_post_types filter hook as a workaround to accomplish this:

    /**
     * Don't track page views if cookie internal_user is set.
     *
     * @param  array $post_types
     * @return array
     */
    function maybe_do_not_track_views($post_types) {
    	if ( isset($_COOKIE['internal_user']) ) {
    		$post_types = array('do_not_track');
    	}
    
        return $post_types;
    }
    add_filter('wpp_trackable_post_types', 'maybe_do_not_track_views', 10, 1);
    Thread Starter sekedus

    (@sekedus)

    Doesn’t work, because my site use cache (LiteSpeed Cache).

    is there another solution?

    Plugin Author Hector Cabrera

    (@hcabrera)

    No, unfortunately that’s the only solution I could think of. Looks like you will have to come up with a different solution.

    If you have any further questions please let me know.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude internal traffic with cookie’ is closed to new replies.