• I have a simple function to track the last viewed post/page by using the action add_action( ‘wp’,’myfunction’);
    That seems to work good. However, it looks like either background processes or other processes view posts/pages of my website as well. Myfunction tells me when a post has been viewed and it seems that that is not only when I (or a user) view a post or page.

    Also tried the hooks loop_start and loop_end, but with the same effect: looks like there are ghost views at random times.

    Does anyone has an idea what can be the matter, the cause or how to prevent/solve this?

    Looking forward to any help.

    hans

Viewing 8 replies - 1 through 8 (of 8 total)
  • Why not just use google analytics or a similar service to track page views?

    You may be seeing traffic from search engine bots like Google that are indexing your website. Most WordPress stats systems have the ability to filter our search engine bots.

    This is the first thing that pops into my head when you say you are getting “ghosted” hits to a page or post.

    You may find this article about the firing order of the hooks useful:

    https://rachievee.com/the-wordpress-hooks-firing-sequence/

    Thread Starter hansstavleu

    (@hansstavleu)

    Thanks so far for your responses!

    1. Yes, I do use Google analytics for page views.
    2. Am afraid indeed that the ghost views are from search engine bots.

    The reason I like to track the view of a post or page myself is that I like to show on the front page of my site the last viewed post or page. See https://curiozy.net

    It would be a little weird when ‘ghosted’ views or other processes interfere with the integrity of it.

    Hans

    Thread Starter hansstavleu

    (@hansstavleu)

    For those who are interested in how I use the add_action. Perhaps it gives an idea about a (possible) solution of the issue or an other approach to keep track of the last viewed post/page.

    https://curiozy.net

    add_action('wp','setLastViewed');
    function setLastViewed() {
    	$storeID = get_option( 'page_on_front' );
    	$pageNotFoundID = get_option ('page-not-found');
    	$LV_Url = get_site_url() . $_SERVER['REQUEST_URI'];
    	$LV_ID = get_the_ID();
    	if ( $LV_ID == $storeID || $LV_ID == $pageNotFoundID) {
    		return;
    	}
    	$LV_Title = get_post($LV_ID)->post_title;
    	$LV_PoP = 'post';
    	if ( is_page() ) {
    		$LV_PoP='page';
    	}
    	update_post_meta($storeID, 'LastViewedPoP', $LV_PoP);
    	update_post_meta($storeID, 'LastViewedUrl', $LV_Url);
           	update_post_meta($storeID, 'LastViewedTitle', $LV_Title);
           	update_post_meta($storeID, 'LastViewedID', $LV_ID);
    
    	// IFTTT "MAKER" TRIGGER FOR MESSAGING PHONE/WATCH WITH THE USE OF PUSHBULLET
    	$time = current_time('H:i');
    	$url = esc_url("https://maker.ifttt.com/trigger/lastviewed/with/key/mycodeishere/?value1=" . $time . ' ' . $LV_PoP . ' ' . $LV_Title);
    	$ch = curl_init($url);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	curl_exec($ch);
    	curl_close($ch);
    	// END-OF-TRIGGER "MAKER"
    }

    Hans, I am not a moderator, but I do know they prefer to keep the forum as clean as possible. In the future please post code using a service such as pastebin.

    I have reviewed your code and it doesnt really seem to be an issue with your code. At least not to me.

    If I were you I’d be checking the servers configuration!

    Thread Starter hansstavleu

    (@hansstavleu)

    Thank you Davood… never heard of pastebin ??

    hans

    No problem! Happy to help.

    That’s what the support community is for.

    Thread Starter hansstavleu

    (@hansstavleu)

    True. It is a valuable good of the community.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘hooking to track the view of a post/page’ is closed to new replies.