Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could someone please provide a quick example how to record this action? #842

Closed
netconstructor opened this issue Apr 18, 2016 · 3 comments
Closed

Comments

@netconstructor
Copy link

This is the third time over the past year I have come back to this plugin, primarily due to its slick visual organization while meeting nearly 98% of my requirements. The problem I am having here is that I simply can't figure out the proper method to create a custom action trigger entry for my specific situation.

Let me point out in advance that I am well aware that what I aim to track would likely be incapable of scaling properly to more than a few concurrent users at a time and this is perfectly fine for my situation... I simply need a basic example of how it can be done.

The nature of my situation involves a custom wordpress website which is only accessible to about 50 individuals each of which need an existing pre approved wp user account to access the site. What I need to be able to do here is simply record every single page view on this domain for each of these users. Ideally I would like the information which gets recorded to include very basic information including date/timestamp, page title, post type name, any associated categories, tags, taxonomies and potentially some specific custom field value. The purpose of these logged pageviews simply need to allow the user to view his own activity and filter/sort it and allow an admin to do the same for all or a single user.

Could someone who has some basic experience with this plugin please provide a basic example of how to achieve this basic functionality?

Thanks in advance!

@lukecarbis
Copy link
Contributor

lukecarbis commented Apr 19, 2016

Hi @netconstructor .

Lack of good docs is a very real problem. We're trying to find the time to prioritise it, amongst other things.

See these related issues: #819, #821

In terms of a starting place, I would recommend having a look at how the Media connector works:
https://github.com/xwp/stream/blob/develop/connectors/class-connector-media.php

Basically you need the following properties:
$name (string), $actions (array)

And the following methods:
get_label(), get_action_labels(), get_context_labels()

Then, for each of the actions in your $actions array, add a callback function, where you log:

function callback_action_name() {
    $this->log(
          'Sprintf style message %s %s',
          array( 'foo' => $foo, 'bar' => $bar ), // Args used for the sprintf message above
          $object_id, // May be null
          $context, // This is the context slug, the label is retrieved based on the slug via get_ context_labels()
          $action // This is the action slug, the label is retrieved based on the slug via get_action_labels()
    );
}

Finally, register your connector class by filtering wp_stream_connectors. See https://github.com/xwp/stream/blob/develop/classes/class-connectors.php#L100-L105

I hope that's enough to get you started. Let me know how you go, and if you need any help.

@Klsneha
Copy link

Klsneha commented Apr 11, 2019

Hi Luke, how do I register connector. I have created a custom connector class and followed the steps given in the wiki of creating custom connecor. Unable to register the connector. What do you mean by To register a Connector, hook into the wp_stream_connectors filter from inside your plugin.

@lkraav
Copy link
Contributor

lkraav commented Apr 11, 2019

Example

<?php                                                                                                                                                                                                                                         
/**                                                                                                                                                                                                                                           
 * Stream integration                                                                                                                                                                                                                         
 *                                                                                                                                                                                                                                            
 * @package Integrations                                                                                                                                                                                                                      
 * @see https://github.com/xwp/stream/wiki/Creating-a-Custom-Connector#registering-a-connector                                                                                                                                                
 * @since 2017.12.22                                                                                                                                                                                                                          
 */                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                              
namespace Integrations;
                                                                                                                                                                                                                                              
defined( 'ABSPATH' ) || exit;                                                                                                                                                                                                                 
                                                                                                                                                                                                                                              
/**                                                                                                                                                                                                                                           
 * Main class                                                                                                                                                                                                                                 
 */                                                                                                                                                                                                                                           
class Stream {                                                                                                                                                                                                                                
                                                                                                                                                                                                                                              
    /**                                                                                                                                                                                                                                       
     * Constructor                                                                                                                                                                                                                            
     */                                                                                                                                                                                                                                       
    public function __construct() {                                                                                                                                                                                                           
        $this->init();                                                                                                                                                                                                                        
    }                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                              
    /**                                                                                                                                                                                                                                       
     * Init                                                                                                                                                                                                                                   
     */                                                                                                                                                                                                                                       
    private function init() {                                                                                                                                                                                                                 
                                                                                                                                                                                                                                              
        add_filter( 'wp_stream_connectors', function( $classes ) {                                                                                                                                                                            
                                                                                                                                                                                                                                              
            $classes['posts-viewed'] = new Stream\ConnectorPostsViewed();                                                                                                                                                                     
                                                                                                                                                                                                                                              
            return $classes;                                                                                                                                                                                                                  
                                                                                                                                                                                                                                              
        } );                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                              
    }                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                              
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants