• Hello. Wondering if it is there a function to track views in an ajax based site. I mean auto loading posts.

    • This topic was modified 4 years, 6 months ago by vicetias.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Ajay

    (@ajay)

    Is there a way for it to call the tracker function?

    I’m not sure if this works, but you could try to call tptn_enqueue_scripts()

    e.g. something like this

    
    add_action( 'wp_enqueue_scripts', 'tptn_enqueue_scripts' );
    
    Thread Starter vicetias

    (@vicetias)

    Thank u so much, but I don’t know much about coding. That code goes in functions.php?

    • This reply was modified 4 years, 6 months ago by vicetias.
    Plugin Author Ajay

    (@ajay)

    Not that code, but you’ll need to find a replacement for what is being called when the next post is loaded.

    Are you using some kind of plugin for this?

    Thread Starter vicetias

    (@vicetias)

    Actually the template has its own ALP plugin. This is the theme I am using.

    • This reply was modified 4 years, 6 months ago by vicetias.
    • This reply was modified 4 years, 6 months ago by vicetias.
    Plugin Author Ajay

    (@ajay)

    Are you able to check with the theme author on how you can trigger or call the tracking script.

    Probably something like this but am not sure if that would work.

    
    add_action( 'the_content', 'tptn_enqueue_scripts' );
    
    Thread Starter vicetias

    (@vicetias)

    My support on Envato expired 🙁 but I don’t think the author could help because he doesn’t help to this kind of things.

    I found this inside the plugin if it helps:

    <?php
    namespace AutoLoadPosts;
    
    class Plugin
    {
    	public function Init()
    	{
    		add_action('plugins_loaded', array($this, 'LoadTextDomain'));
    		add_action('wp_enqueue_scripts', array($this, 'RegisterStyles'));
    		add_action('wp_enqueue_scripts', array($this, 'RegisterScripts'));
    		
    		$RelatedPostsWidget = new RelatedPostsWidget();
    		$RelatedPostsWidget->init();
    		
    //		$RentReceipt = new RentReceipt();
    //		$RentReceipt->Init();
    	}
    	
    	public function LoadTextDomain()
    	{
    		load_plugin_textdomain(ALP_PLUGIN_DOMAIN, false, ALP_PLUGIN_LANGUAGES_REL_PATH);
    	}
    	
    	public function RegisterStyles()
    	{
    		wp_enqueue_style('ql-jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css');
    		wp_enqueue_style('ql-main', ALP_PLUGIN_STYLES_URL . 'main.css', array('ql-bootstrap', 'ql-jquery-ui'));
    	}
    	
    	public function RegisterScripts()
    	{
    		wp_enqueue_script('jquery');
    		wp_enqueue_script('jquery-ui-core');
    		
    		//wp_enqueue_script('jquery-history', ALP_PLUGIN_SCRIPTS_URL . 'jquery.history.js', array('jquery'), false, true);
    		wp_enqueue_script('jquery-sticky-kit', ALP_PLUGIN_SCRIPTS_URL . 'jquery.sticky-kit.min.js', array('jquery'), false, true);
    		wp_enqueue_script('jquery-waypoints', ALP_PLUGIN_SCRIPTS_URL . 'jquery.waypoints.min.js', array('jquery'), false, true);
    		
    		$data = array();
    		$data['ajaxurl'] = admin_url('admin-ajax.php');
    		$params = array(
    			'l10n_print_after' => 'alp_config = ' . json_encode($data) . ';'
    		);
    		
    	}
    }
    //jQuery(document).ready(function($) {
    jQuery(window).on('load', function() {
    	var $ = jQuery;
    	var nextPostFlag = false;
    	
    	if(!$('body').hasClass('single-post'))
    		return;
    	
    	if($('article').eq(0).length)
    		$('article').eq(0).addClass('first');
    	
    	createStickyWidgetArea();
    	
    	var $nextPost = $('.alp-related-post').eq(1);
    	if($nextPost.length)
    		$nextPost.addClass('load-next');
    	
    	createLastArticleWaypoint();
    	
    	$(window).on('scroll', findCurrentArticle);
    	$(window).on('resize', findCurrentArticle);
    	$(window).trigger('resize');
    	
    	function loadRelatedPosts()
    	{
    		if(nextPostFlag)
    			return;
    		
    		$nextPost = $('.alp-related-posts .alp-related-post.load-next');
    		if(!$nextPost.length)
    			return;
    		
    		//get next article
    		nextPostFlag = true;
    		$.get($nextPost.find('.post-title').attr('href'), {}, function(data) {
    			var postTitle=$(data).filter('title').text();
    			$nextPost.attr('data-document-title', postTitle);
    			var $newArticle = $(data).find('article');
    			if(!$newArticle.length)
    				return;
    			
    			if($('article#' + $newArticle.attr('id')).length)
    				return;
    			
    			$('article:last-of-type').after($newArticle);
    			
    			Waypoint.destroyAll();
    			$('.alp-related-post').removeClass('load-next');
    			
    			if($nextPost.next().length)
    				$nextPost.next().addClass('load-next');
    			
    			$('article img').load(function() {
    				createLastArticleWaypoint();
    				$(window).trigger('resize');
    				nextPostFlag = false;
    			});
    		}, 'html');
    	}
    	
    	function createStickyWidgetArea()
    	{
    		if(!$('.widget_alp-related-posts').length)
    			return;
    		$('.widget_alp-related-posts').stick_in_parent({
    			offset_top: 100,
    			recalc_every: 1,
    			bottoming: true
    		});
    	}
    	
    	function findCurrentArticle()
    	{
    		var windowTop = $(window).scrollTop();
    		var windowHeight = $(window).height();
    		var windowBottom = windowTop+windowHeight;
    		
    		var $article, 
    			articleTop, 
    			articleHeight, 
    			articleBottom,
    			$widgetPost,
    			advertHtml,
    			offset = 300;
    		
    		$('article').each(function() {
    			$article = $(this);
    			articleTop = $article.offset().top;
    			articleHeight = $article.height();
    			articleBottom = articleTop+articleHeight;
    			
    			if(articleTop-offset<windowTop && articleBottom-offset>windowTop && 
    				('post-'+$('.alp-related-post.current').attr('data-id'))!=$article.attr('id'))
    			{
    				$('.alp-related-post').removeClass('current');
    				$widgetPost = $('.alp-related-post.' + $article.attr('id'));
    				$widgetPost.addClass('current');
    				
    				advertHtml = $('.alp-related-posts .alp-advert')[0].outerHTML;
    				$('.alp-related-posts .alp-advert').remove();
    				$widgetPost.after(advertHtml);
    				
    				history.pushState(null, $widgetPost.attr('data-document-title'), $widgetPost.find('.post-title').attr('href'));
    				if($article.attr('pageview')!='1' && 
    					!$article.hasClass('first'))
    				{
    					var pageViewData={
    						hitType: 'pageview',
    						title: document.title,
    						page: location.pathname
    					};
    					//console.log('pageview');
    					//console.log(pageViewData);
    					ga('send', pageViewData);
    					$article.attr('pageview', 1)
    				}
    				
    				//$('.alp-related-post.' + $article.attr('id')).addClass('current');
    				
    				$('.alp-related-post').show();
    				$('.alp-related-post').each(function() {
    					if($(this).hasClass('current'))
    						return false;
    					$(this).hide();
    				});				
    				return false;
    			}
    		});
    	}
    	
    	function createLastArticleWaypoint()
    	{
    		var $lastArticle = $('article:last-of-type')[0];
    		var waypoint = new Waypoint({
    			element: $lastArticle,
    			handler: function(direction) {
    				loadRelatedPosts();
    			},
    			//offset: 'bottom-in-view'
    			offset: function() {				
    				//taken from waypoit.js 'bottom-in-view' function
    				return this.context.innerHeight() - this.adapter.outerHeight()+750;
    			}
    			//offset: '80%'
    		})
    	}
    	
    });
    

    If you want I can also send you the zip archive.

    • This reply was modified 4 years, 6 months ago by vicetias.
    • This reply was modified 4 years, 6 months ago by vicetias.
    • This reply was modified 4 years, 6 months ago by vicetias.
    Plugin Author Ajay

    (@ajay)

    @vicetias

    Sorry for the delay in responding. I’ve looked at the code and I have to admit it is way above my knowledge levels to edit.

    I suspect you’ll need to speak to someone who is a JS/jquery expert.

    Thread Starter vicetias

    (@vicetias)

    Well, ok, don’t worry!

    Another question: I’m trying to get the post views without “.”. I mean 1.000 => 1000.

    What can I add to this code?

    update_post_meta( $postID, 'total_views', get_tptn_post_count_only( $postID, 'total' ) );
    update_post_meta( $postID, 'daily_views', get_tptn_post_count_only( $postID, 'daily' ) );

    And is there another period range to collect views? (weekly, monthly, yearly? I know there is a “custom period”, but I want to keep it to daily.

    I use this to save the views data to postmeta because I think it’s the only way to order the popular posts into a php archive. If there another way to do it without saving to postmeta, please let me know.

    This is the code:

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

    • This reply was modified 4 years, 5 months ago by vicetias.
    • This reply was modified 4 years, 5 months ago by vicetias.
    • This reply was modified 4 years, 5 months ago by vicetias.
    • This reply was modified 4 years, 5 months ago by vicetias.
    • This reply was modified 4 years, 5 months ago by vicetias.
    • This reply was modified 4 years, 5 months ago by vicetias.
    Plugin Author Ajay

    (@ajay)

    Look at this for ordering: https://gist.github.com/ajaydsouza/c8defd4b46e53240e376#file-tptn-api-examples-php

    You won’t need to save them into the meta key.

    For the number formatting – the plugin automatically number formats the code, you can choose to remove this / edit it: https://gist.github.com/ajaydsouza/d0ddf2461cebab09cc38eb5ea0697cae
    You just need line 11 in there – the rest was to convert it to k, m, etc.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Ajax/ Auto loading posts’ is closed to new replies.