How do I request the number of number views to publish in Wordpress JetPack?

I use JetPack statistics to keep track of my blog statistics. I would like to extract the 10 most popular posts for a given period (like last month).

I used the WordPress plugin API that worked well, but after upgrading to JetPack it no longer works.

Is there an API that allows me to request the number of view views for a message?

+4
source share
1 answer

Inside the database

This option is written to the database and used by toolbar widgets:

get_option( 'stats_cache' ); 

It returns an array like this:

 array( ['7375996b7a989f95a6ed03ca7c899b1f'] => array( [1353440532] => array( [0] => array( ['post_id'] => 0 ['post_title'] => 'Home page' ['post_permalink'] => 'http://www.example.com/' ['views'] => 1132 ) [1] => array( ['post_id'] => 4784 ['post_title'] => 'Hello World!' ['post_permalink'] => ['views'] => 493 ) /* till item [9] */ 

WordPress.com request

You can call the following Jetpack function:

 if( function_exists( 'stats_get_csv' ) ) { $top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' ); } 

What returns:

 array( [0] => array( ['post_id'] => 0 ['post_title'] => 'Home page' ['post_permalink'] => 'http://www.example.com/' ['views'] => 6806 ) [1] => array( ['post_id'] => 8005 ['post_title'] => 'Hello World!' ['post_permalink'] => ['views'] => 1845 ) /* till item [29] */ 

Get_stats_csv function

/plugins/jetpack/modules/stats.php

The get_stats_csv function calls http://stats.wordpress.com/csv.php . If we visit this address, we will get this answer:

 Error: api_key is a required parameter. Required parameters: api_key, blog_id or blog_uri. Optional parameters: table, post_id, end, days, limit, summarize. Parameters: api_key String A secret unique to your WordPress.com user account. blog_id Integer The number that identifies your blog. Find it in other stats URLs. blog_uri String The full URL to the root directory of your blog. Including the full path. table String One of views, postviews, referrers, referrers_grouped, searchterms, clicks, videoplays. post_id Integer For use with postviews table. end String The last day of the desired time frame. Format is 'Ymd' (eg 2007-05-01) and default is UTC date. days Integer The length of the desired time frame. Default is 30. "-1" means unlimited. period String For use with views table and the 'days' parameter. The desired time period grouping. 'week' or 'month' Use 'days' as the number of results to return (eg '&period=week&days=12' to return 12 weeks) limit Integer The maximum number of records to return. Default is 100. "-1" means unlimited. If days is -1, limit is capped at 500. summarize Flag If present, summarizes all matching records. format String The format the data is returned in, 'csv', 'xml' or 'json'. Default is 'csv'. Non-working query example: ?api_key=123456789abc&blog_id=155&table=referrers&days=30&limit=-1&summarize Result format is csv with one row per line and column names in first row. Strings containing double quotes, commas, or "\n" are enclosed in double-quotes. Double-qoutes in strings are escaped by inserting another double-quote. Example: "pet food" recipe Becomes: """pet food"" recipe" Developers, please cache the results for at least 180 seconds. 
+5
source

Source: https://habr.com/ru/post/1403937/


All Articles