BuddyDev

Search

Recent Visitors For BuddyPress Profile – Showing double visits

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15835

    Goal: check for user type ‘coach’ OR query variable ID (unlogged coach user) and show activity feed alert if validated user visits happen.

    What I’ve done: I’ve added a user activity feed creation action to the end of the “bp_visitors_record_visit()” function. It adds an activity update telling the user their profile has been viewed and by whom but it is creating 2 activity instances and incrementing “visitors_get_profile_visit_count” x2 for each single visit. The “visitors_get_unique_visitors_count” works fine. I have the latest version installed.

    Sidenote: I tried using the “visitors_profile_visited” action in my functions.php but the visitor_id was not passed in the function so it didn’t work.

    At the start of the bp_visitors_record_visit() I have this:

    
    if ( isset( $_GET['referrer_coach'] ) && is_numeric( $_GET['referrer_coach'] ) ) {
    		$email_user = $_GET['referrer_coach']; // Smashing Digital //
    	}
    
    	if ( empty($email_user) ) { // Smashing Digital //
    		// do not try to record if it is not a user profile or the user is not logged in or viewing self profile.
    		if ( ! bp_is_user() || ! is_user_logged_in() || bp_is_my_profile() ) {
    			return;
    		}
    	}
    
    	if ( ! empty($email_user) && ! bp_is_user() ) {
    		return;
    	}
    
    	// do not try to record if it is not a coach user profile 
    	if ( is_user_logged_in() && ! bp_has_member_type( bp_loggedin_user_id(), 'coach' ) ) {
    		return;
    	}
    
    	$visitor_id = empty($email_user) ? get_current_user_id() : $email_user; 
    	// $visitor_id = get_current_user_id();
    

    At the end of the bp_visitors_record_visit() I have this:

    
    /**
    	 
      do_action( 'visitors_profile_visited', $user_id, $visitor_id );
      
      // An array of arguments. 
      $coach_user_id = $visitor_id;
      $coach_name = xprofile_get_field_data( 1, $coach_user_id );
      $activity_action = 'Coach ' . $coach_name . ' viewed your profile! ';
      $activity_args = array(
        'action' => $activity_action, // The activity action - e.g. "Jon Doe posted an update" 
        'content' => '', // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!" 
        'component' => 'visitors', // The name/ID of the component e.g. groups, profile, mycomponent. 
        'type' => 'coach_visit', // The activity type e.g. activity_update, profile_updated. 
        'user_id' => $user_id, // Optional: The user to record the activity for, can be false if this activity is not for a user. 
        'item_id' => false, // Optional: The ID of the specific item being recorded, e.g. a blog_id. 
        'secondary_item_id' => false, // Optional: A second ID used to further filter e.g. a comment_id. 
        'recorded_time' => bp_core_current_time(), // The GMT time that this activity was recorded. 
        'hide_sitewide' => true, // Should this be hidden on the sitewide activity stream? 
      );   
      $result = bp_activity_add( $activity_args );
    
  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15836

    Also, I’m sure there is a more elegant way to structure that conditional logic to check for query variable user id OR user of type “x”. The ugly logic I put together appears to work properly.

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15876

    Update: I got the add_action function to work properly. Even if I remove my query string variable logic the system is still creating every notification and activity comment 2x or 3x per count. Also the total user counts are incremented by 2x or 3x each view.

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #15877

    Hi Leland,
    Allow me to check your code a bit later today and assist you. Most probably you are calling the recorder as well as the auto view recorder is getting called too.

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15892
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15910
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #15924
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15935
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #15938
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15944
    This reply has been marked as private.

You must be logged in to reply to this topic.

This topic is: not resolved