jeherve / jp-post-views

Display the number of views for each one of your posts, as recorded by Jetpack Stats.
https://wordpress.org/plugins/post-views-for-jetpack/
3 stars 0 forks source link

Store more information about each post in post meta #9

Open jeherve opened 4 years ago

jeherve commented 4 years ago

Since the REST API gives us a good set of information about each post, I could see about storing that information in post meta, since I make the query already anyway: https://github.com/jeherve/jp-post-views/blob/354fb18a4797feddb82104606f52c7b2b7df7dbc/functions.jp-post-views.php#L34

I am not sure what I could do with that information yet though.

Suggested here: https://wordpress.org/support/topic/sort-popular-posts-by-meta-key/

jeherve commented 3 years ago

An additional idea here:

https://wordpress.org/support/topic/option-for-time-period/

I would like to have options to display statistics for periods of time, such as recent year and recent month.

See the full response here for an example of what could be done: https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/post/%24post_id/

jeherve commented 2 years ago

An additional idea here: https://wordpress.org/support/topic/views-today-shortcode/

Is there any option to display today’s website statistics as a shortcode? I want to insert it into the page

I ended up creating a new shortcode to display today's data for them:

add_shortcode(
    'jp_post_view_today',
    function () {
        // Get the post ID.
        $post_id = get_the_ID();

        if ( ! isset( $post_id ) || empty( $post_id ) ) {
            return;
        }

        if ( ! function_exists( 'stats_get_from_restapi' ) ) {
            return;
        }

        // Get extra views for that post ID.
        $stats = stats_get_from_restapi(
            array( 'fields' => 'views' ),
            sprintf(
                'post/%d',
                $post_id
            )
        );

        $today_data = array();
        if (
            isset( $stats )
            && ! empty( $stats )
            && isset( $stats->data )
        ) {
            $today_data = end( $stats->data );
        }

        if ( empty( $today_data ) || ! isset( $today_data[1] ) ) {
            return esc_html__( 'no views', 'post-views-for-jetpack' );
        }

        return sprintf(
            esc_html(
                _n(
                    '%s view today',
                    '%s views today',
                    $today_data[1],
                    'post-views-for-jetpack'
                )
            ),
            number_format_i18n( $today_data[1] )
        );
    }
);