Support » Plugin: Toplytics » Post views

  • Resolved vicetias

    (@vicetias)



    Hi again! The plugin is working really good at the moment but I have a question about the views.

    Is there any function to get the post views count? Look, my theme uses this code to show popular posts in a php template:

    <?php $pop_days = esc_html(get_option('mvp_pop_days')); $pop_num = esc_html(get_option('mvp_pop_num')); $popular_days_ago = "$pop_days days ago"; $recent = new WP_Query(array('posts_per_page' => $pop_num, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>

    I think the only thing I need is change: ‘post_views_count’ to ‘-ga:pageviews’ ??? But I know that I need a function for it, because the theme uses this function to get the post views:

    if ( !function_exists( 'getCrunchifyPostViews' ) ) {
    function getCrunchifyPostViews($postID){
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if($count==''){
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            return "0 View";
        }
        return $count.' Views';
    }
    }
    
    if ( !function_exists( 'setCrunchifyPostViews' ) ) {
    function setCrunchifyPostViews($postID) {
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if($count==''){
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
        }else{
            $count++;
            update_post_meta($postID, $count_key, $count);
        }
    }
    }

    It’s a trashy way to count the post views and that’s why I switched to your plugin!

    • This topic was modified 1 year, 8 months ago by  vicetias.
    • This topic was modified 1 year, 8 months ago by  vicetias.
    • This topic was modified 1 year, 8 months ago by  vicetias.
    • This topic was modified 1 year, 8 months ago by  vicetias.
    • This topic was modified 1 year, 8 months ago by  vicetias.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Presslabs

    (@presslabs)

    Hi Vicetias,

    In Toplytics the logic is different because the results http://tendencilandia.com/toplytics.json are only for the top posts therefore if you use the data from Toplytics in a template file you must be aware that not all posts will have the post views available so you must use an ‘if’ statement and sometimes (when the post is not in top) the post views will not be available in template. Anyway, if you still want to use Toplytics results for this, I strongly advise you to use only JS code(avoid using PHP) and this code will help you to get your post views as you wish https://github.com/PressLabs/toplytics/blob/master/toplytics/js/toplytics.js you just need to change it a little.

    If you want to use a better solution for your post views in template you can try this plugin (Google Analytics Post Pageviews)[https://wordpress.org/plugins/google-analytics-post-pageviews/] the logic of this plugin is exactly what you try to make in your theme.

    If you need more help just let me know.

    Thanks,

    Thank you for your support! Google Analytics Post Pageviews didn’t work anymore at least for me.

    I tried with the codes you provided on ‘Faq’ and it shows the default look in a php template (i can’t add div class on it), unlike the widget because I added css style and looks like this:

    And now, that code (https://github.com/PressLabs/toplytics/blob/master/toplytics/js/toplytics.js) where I must put it? In functions? Sorry I don’t know nothing about JS and I think i’m still need a php file because these pages have a template name: ‘home’, ‘latest’ in order to show it properly, then I created ‘popular’ template

    <?php
    	/* Template Name: Populares */
    ?>
    <?php get_header(); ?>
    • This reply was modified 1 year, 8 months ago by  vicetias.
    • This reply was modified 1 year, 8 months ago by  vicetias.
    Plugin Author Presslabs

    (@presslabs)

    Here is a simple example of using the toplytics JS code inside of the index.php template https://olarmarius.tk/

    <!DOCTYPE html><html><head><title>::</title><style>#widget {
      background-color: lime;
      width: 100%;
      height: 400px;
    }</style></head><body><div id="widget"></div> <script>function toplytics_get_data( args, callback ) {
        var xmlhttp;
        if ( window.XMLHttpRequest ) {
            xmlhttp = new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
        } else {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); // code for IE6, IE5
        }
    
        xmlhttp.onreadystatechange = function() {
            if ( xmlhttp.readyState === 4 && xmlhttp.status === 200 ) {
                var toplytics_json_data = JSON.parse(xmlhttp.responseText);
                callback( args, toplytics_json_data );
            }
        };
        xmlhttp.open('GET', 'https://olarmarius.tk/toplytics.json/', true);
        xmlhttp.send();console.log('after xmlhttp.send();');
    }
    
    function toplytics_results( toplytics_args ) {console.log('here...');
        toplytics_get_data( toplytics_args, function( args, toplytics_json_data ) {console.log('toplytics_json_data...');
            var html    = '';
            var results = toplytics_json_data[args.period];
            var counter = 0;
            for ( var index in results ) {
                if ( results.hasOwnProperty( index ) ) {
                    var permalink = results[ index ].permalink;
                    var title     = results[ index ].title;
                    //var post_id   = results[ index ].post_id;
                    var views     = results[ index ].views;
                    counter++;
                    if ( counter > args.numberposts ) { break; }
    
                    var views_html = '';
                    if ( args.showviews ) {
                        views_html = '<span class="post-views">' + views + ' views</span>';
                    }
    
                    if ( permalink && title ) {
                        html = html + '<li><a href="' + permalink + '">' + title + '</a>&nbsp;' + views_html + '</li>';
                    }
                }
            }
            document.getElementById( args.widget_id ).innerHTML = '<ol>' + html + '</ol>';
        });
    }
    
    toplytics_args = {
    	'period' : 'month',
    	'numberposts' : 3,
    	'showviews' : true,
      'widget_id' : 'widget'
    }
    
    toplytics_results( toplytics_args );</script> </body></html>
    • This reply was modified 1 year, 8 months ago by  Presslabs.
    • This reply was modified 1 year, 8 months ago by  Presslabs.
    • This reply was modified 1 year, 8 months ago by  Presslabs.

    I can’t 🙁 I need to keep this:

    <?php
    	/* Template Name: Popular */
    ?>
    <?php get_header(); ?>

    I added the code into the “scripts” tags but nothing appears and the only type of code that works is this but with the default look (see the screenshoot above):

    <?php
        if ( function_exists( 'toplytics_get_results' ) ) {
            $toplytics_args = array(
                'period' => 'year',  // default=month (today/week/month)
                'numberposts' => 100    // default=5 (min=1/max=250)
            );
            $toplytics_results = toplytics_get_results( $toplytics_args );
            if ( $toplytics_results ) {
                $k = 0;
                foreach ( $toplytics_results as $post_id => $post_views ) {
                    echo (++$k) . ') <a href="' . get_permalink( $post_id )
                        . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">'
                        . get_the_title( $post_id ) . '</a> - ' . $post_views . ' Views<br />';
                }
            }
        }
    ?>

    The only I should need is adding css style like I did in template.php but apparently is imposible. Any way to do that?

    Plugin Author Presslabs

    (@presslabs)

    Dear Vicetias, the above code was just an example is not the solution, you must adjust it to fit your needs. I don’t guarantee that the example works with copy-paste. As you can see I add all the HTML code in order to show up how all things work together.

    Let me explain it a little:

    First you create a ‘div’ HTML element like in example(with an unique ID):
    <div id=”unique-id”></div>

    Second, after you created the ‘div’ HTML element, you have to load the JS code which will load the toplytics.json file.

    Third you have to compose your HTML code using ‘li’ HTML tags like in the example above.

    Notice:

    The CSS code of the ‘unique-id’ have to be built by you according with your entire HTML design.

    The reason I put it in index.php is just because it is the first template of the theme, you must put it in your template.

    Very important is to use JS code not PHP because if you use the PHP code you will break the cache system of your hosting and you will have problems on high traffic.

    About your example of PHP code and your problem with CSS, you have to add a ‘div’ HTML tag above your PHP code, with a unique id and then try to adjust the style for this id in style.css I don’t see any problem of adding your own style for PHP code?

    Another important thing is to understand that the widget used as WP let to be used(select Appearance->Widgets then drag and drop) the Toplytics Widget will have the default css from the active theme. If you want a custom CSS you must Inspect the HTML code resulted, find the widget-id and then add the custom CSS into style.css

    I hope this helped you. If you need more help let me know.
    Thanks

    • This reply was modified 1 year, 8 months ago by  Presslabs.

    Thanks, I got it with the JS code you provided, I used an unique ID and all is ok, the only thing I can’t get is the post thumbnail of every link like in the widget.

    I tried with that php code too and for me it loads faster than the JS code (but you say that’s better). I putted div tags and it looks good but the only thing I need is delete the number before the link, I tried deleting echo (++$k) . ‘) but I got error. What I need to delete?

    if ( $toplytics_results ) {
                $k = 0;
                foreach ( $toplytics_results as $post_id => $post_views ) {
                    echo (++$k) . ')
    • This reply was modified 1 year, 7 months ago by  vicetias.
    Plugin Author Presslabs

    (@presslabs)

    Try like this:

    
    if ( $toplytics_results ) {
                $k = 0;
                foreach ( $toplytics_results as $post_id => $post_views ) {
                    echo '
    
    Plugin Author Presslabs

    (@presslabs)

    Hi! Thank you for your interest in Toplytics. I am afraid we will close this thread here as we have answered your questions regarding our plugin, but if you have any further questions on Toplytics, please do not hesitate to contact us.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Post views’ is closed to new replies.