BuddyDev

Search

User view count partially not working

  • Participant
    Level: Initiated
    Posts: 16
    Nushara on #28150

    I have a very strange issue occurring. Some users simply don’t get updated view counts, while others are working just fine. So the function itself seems to be working all right.

    What is also very strange is that this is working fine on my development server but some users on prod are not getting any updates.

    This is my function adding the database entry. Maybe you can spot any issue with this.

    
    	function set_profile_views( $profile_ID ) {
    
    	 	// The user's IP address
    	    $user_ip = $_SERVER['REMOTE_ADDR'];
    
    	    $ip_key = 'profile_views_ip'; // The IP Address post meta key
    	    $views_key = 'profile_views_count'; // The views post meta key
    
    	    // The current post views count
    		$count = get_post_meta( $profile_ID, $views_key, true );
    		
    		//Check day
    		$day = date("l");
    		$day_table = 'profile_views_day'; // Set the day to check
    		$setDay = get_post_meta( $profile_ID, $day_table , true );
    		if ( '' == $count ) {
    			$count = 0;
    			delete_post_meta( $profile_ID, $views_key );
    			add_post_meta( $profile_ID, $views_key, '0' );
    		} elseif ( is_page() ) {
    
    			if('' == $setDay){ //Check if day is set in database
    				add_post_meta( $profile_ID, $day_table , $day );
    			}else{
    				if($day != $setDay){ // Check if day is the same as database
    					update_post_meta( $profile_ID, $day_table , $day );
    					delete_post_meta( $profile_ID, $ip_key );
    				}
    			}
    
    			// Array of IP addresses that have already visited the post.
    			if ( '' != get_post_meta( $profile_ID, $ip_key, true ) ) {
    			    $ip = json_decode( get_post_meta( $profile_ID, $ip_key, true ), true );
    			} else {
    			    $ip = array();
    			}
    
    			// The following checks if the user's IP already exists
    			for ( $i = 0; $i < count( $ip ); $i++ ) {
    			    if ( $ip[ $i ] == $user_ip ) {
    			        return false;
    			    }
    			}
    
    			// Update and encode the $ip array into a JSON string
    			$ip[ count( $ip ) ] = $user_ip;
    			$json_ip = json_encode( $ip );
    
    			// Update the post's metadata
    			$count++;
    			update_post_meta( $profile_ID, $views_key, $count ); // Update the count
    			update_post_meta( $profile_ID, $ip_key, $json_ip ); // Update the user IP JSON obect
    
    		}
    	}
    

    I am very grateful for any help because I am not getting any further with this 🙁

  • Keymaster
    (BuddyDev Team)
    Posts: 22510
    Brajesh Singh on #28154

    Hi Nushara ,
    Thank you for the question.

    Did you get the code form some developer, if yes, Please reconnect with them and ask for some better solution. There are too many things wrong with the above code.
    Also, It should not abuse usermeta for storing viewer ip.

    If your site gets moderate traffic, This is bound to crash the databasae table and also slow down the user queries.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 16
    Nushara on #28159

    This is 90% from Youzer which I can not contact anymore since their Support is not active for me. I then added the clearance routine to make sure the database is not blowing up that much and gets cleared every day at least once.

    But the main structure is done by them.

    Can you tell me how you would normally do something like this?

    Regards
    Mike from Nushara

  • Participant
    Level: Initiated
    Posts: 16
    Nushara on #28160

    I have done some changes to this, you are right there is a lot of wrong with this.

    But even after changing this, it is still not working for these users.

    
    	/**
    	 * # Set Profile Views Number .
    	 */
    	function set_profile_views( $profile_ID ) {
    
    	 	// The user's IP address
    	    $user_ip = $_SERVER['REMOTE_ADDR'];
    	    $ip_key = 'profile_views_ip'; // The IP Address post meta key
    	    $views_key = 'profile_views_count'; // The views post meta key
    
    	    // The current post views count
    		$count = get_post_meta( $profile_ID, $views_key, true );
    		
    		//Check day
    		$day = date("l");
    		$day_table = 'profile_views_day'; // Set the day to check
    		$setDay = get_post_meta( $profile_ID, $day_table , true );
    		$checkDay = metadata_exists('post', $profile_ID, 'profile_views_day');
    
    		//Check if Viewcount and IP Table already exist
    		$check_ip = metadata_exists('post', $profile_ID, 'profile_views_ip');
    		$check_views = metadata_exists('post', $profile_ID, 'profile_views_count');
    
    		//Bank IT-Tables
    		$blank_array = array();
    		$json_array = json_encode( $blank_array );
    
    		if ( is_page() ) {
    			//Check if Views already exist
    			if(!$check_views){
    				add_post_meta( $profile_ID, $views_key, 0 );
    			}
    
    			//Check if IP-Meta already exist
    			if(!$check_ip){
    				add_post_meta( $profile_ID, $ip_key, $json_array );
    			}
    
    			if(!$checkDay){ //Check if day is set in database
    				add_post_meta( $profile_ID, $day_table , $day );
    			}else{
    				if($day != $setDay){ // Check if day is the same as database
    					update_post_meta( $profile_ID, $day_table , $day );
    					update_post_meta( $profile_ID, $ip_key, $json_array);
    				}
    			}
    
    			// Array of IP addresses that have already visited the post.
    			if ( '' != get_post_meta( $profile_ID, $ip_key, true ) ) {
    			    $ip = json_decode( get_post_meta( $profile_ID, $ip_key, true ), true );
    			} else {
    				$ip = array();
    			}
    
    			// The following checks if the user's IP already exists
    			for ( $i = 0; $i < count( $ip ); $i++ ) {
    			    if ( $ip[ $i ] == $user_ip ) {
    			        return false;
    			    }
    			}
    
    			// Update and encode the $ip array into a JSON string
    			$ip[ count( $ip ) ] = $user_ip;
    			$json_ip = json_encode( $ip );
    
    			// Update the post's metadata
    			$count++;
    			update_post_meta( $profile_ID, $views_key, $count ); // Update the count
    			update_post_meta( $profile_ID, $ip_key, $json_ip ); // Update the user IP JSON obect
    		}
    	}
    
  • Participant
    Level: Initiated
    Posts: 16
    Nushara on #28161

    Oh and they are stored on the postmeta, not usermeta.

  • Keymaster
    (BuddyDev Team)
    Posts: 22510
    Brajesh Singh on #28178

    Hi Mike,
    I am sorry but the above code will not work. You can not directly interchange the user id and post id.

    Also, I am sorry, but providing this type of custom code is beyond our forum support.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 16
    Nushara on #28179

    Oh ok, I thought this would be the right place to post something like this.

    I am sorry then. I changed the postmeta to usermeta and updated the code to work pretty well for me.

    This way you always relate to a users ID like all the other user related stuff.

    But thank you for assisting me till this point.

    You have a great day!

    Regards,
    Mike from Nushara

  • Keymaster
    (BuddyDev Team)
    Posts: 22510
    Brajesh Singh on #28199

    Thank you for the understanding. I am glad that is is working.

    We do assist with code but anything that needs more than 15 minutes of coding is beyond our forum support.

    Regards
    Brajesh

The topic ‘User view count partially not working’ is closed to new replies.

This topic is: not resolved