Support » Plugin: WordPress Popular Posts » WPP with an AJax-based site

  • Resolved dkeeling

    (@dkeeling)


    First, thanks for a wonderful plugin.

    I’m trying to implement this on a site that uses Swup (https://github.com/swup/swup/) to load the next page via ajax. Unfortunately, when I load the page that way, the scripts are not executed again to update the view count.

    I looked through the plugin JS, but I wasn’t sure exactly what I needed to do to update the page’s view count manually once loaded. Any direction you can give would be greatly appreciated.

    Thank you!

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi @dkeeling,

    WordPress Popular Posts uses the REST API to track page views, and the plugin uses JS/AJAX for this: there’s a JavaScript object that I believe should be available globally called WordPressPopularPosts that you could use to save you some work. Here’s how the plugin uses it:

    WordPressPopularPosts.post(
        wpp_params.ajax_url,
        "_wpnonce=" + wpp_params.token + "&wpp_id=" + wpp_params.ID + "&sampling=" + wpp_params.sampling_active + "&sampling_rate=" + wpp_params.sampling_rate,
        function( response ){
            wpp_params.debug&&window.console&&window.console.log&&window.console.log( response );
        }
    );

    (see wpp.js for more details.)

    As you can see, the post() method of the object expects three parameters:

    1. The URL of the REST API endpoint WPP uses to track pageviews (eg. https://www.example.com/wp-json/wordpress-popular-posts/v1/popular-posts)
    2. The parameters: _wpnonce, a valid WordPress Nonce called _wpnonce; wpp_id, the post/page ID; sampling, whether Data Sampling is enabled or not on your site (0 = disabled, 1 = enabled); and sampling_rate, the Sampling Rate.
    3. A JS callback function to be executed after updating the views count.

    If you don’t want to use the WordPressPopularPosts object, you can also just make a regular Ajax call using the same exact parameters (url, args, etc) and you should be good to go.

    If you have any further questions don’t hesitate to ask.

    Thanks so much @hcabrera. I had just found your how-to post from a few years ago (https://wordpress.org/support/topic/updating-the-views-count-via-ajax/) and that implementation is working well now — but I might switch to this method if it’s the preferred way of doing things.

    Thanks for the excellent support. I’ve been through many of the other forum posts and github issues and can see how active you are. I’m amazed that this plugin is free, considering how robust it appears to be. Great job!

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Yeah, I’ve thought a few times about monetizing the plugin but seeing that it already provides a lot of features for free, well, I don’t see how that would be possible now haha

    Thanks for the kind words!

    Hello. I need the plugin to count views for auto loading posts, but I don’t now how to implement this. Please help!

    • This reply was modified 1 year, 1 month ago by Matías.
    • This reply was modified 1 year, 1 month ago by Matías.
    Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi @m4tl4s,

    You posted asking for help with that several months ago and then you vanished 😛 My answer from back then still applies, though.

    Lmao yes @hcabrera, because I found another plugin that worked when the auto loading function was enabled in my site. Now it’s disabled and a post got viral and I just realized GA views are double than the plugin for that post, so I need back to your amazing plugin… I think it’s the only one that counts very similar to GA 😀

    And I’m thinking in contact you when I enable ALP again. Además considerando que sé que hablas español, mejor aún!

    Firstly, I want to know if can I import the data from this plugin?

    • This reply was modified 1 year, 1 month ago by Matías.
    • This reply was modified 1 year, 1 month ago by Matías.
    Plugin Author Héctor Cabrera

    (@hcabrera)

    Seeing that coding isn’t apparently your forte, my offer from back then still stands @m4tl4s 😉

    Thank you, and re-read the edited version of the message and answer, please!

    Plugin Author Héctor Cabrera

    (@hcabrera)

    I’d need to install that plugin and have a look to see how it works. If it saves views data to the database (and not some external service, for example) then it should be possible to import its data into WPP (importing from an external service is doable as well but it usually requires a bit more work). Anyways this part of the discussion is off-topic for this thread, please open a new thread and/or contact me directly if you wish to further talk about it.

    Y sí, hablo español por supuesto 😛

    Sorry for that. I sent you a message to your page.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WPP with an AJax-based site’ is closed to new replies.