• Resolved janvitos

    (@janvitos)


    Hi,

    It’s nice that this plugin already provides support for tracking page views in Google Analytics. Though, it doesn’t seem to support the new gtag.js tracking method mentioned here: https://developers.google.com/analytics/devguides/collection/gtagjs/

    I added support for gtag.js by creating this code snippet:

    
    $('body').on( 'alnp-post-changed', function( e, post_title, post_url, post_id ) {
    	post_url = post_url.replace(/https?:\/\/[^\/]+/i, '');
    	gtag('config', 'UA-XXXXXXXX-X', {
    		'page_title' : post_title,
    		'page_path': post_url
    	});
    });
    

    To make it work, you’ll need to replace UA-XXXXXXXX-X with your own Google Analytics tracking ID.

    I think it could be a nice feature to add in the plugin in the future.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Sébastien Dumont

    (@sebd86)

    It’s an interesting idea and thank you for all the feedback you have provided so far @janvitos.

    Do you use another plugin to install the gtag javascript?

    Thread Starter janvitos

    (@janvitos)

    No plugin, I simply use Google’s method to include it in my site’s header.

    I put a link in my previous comment to the Google documentation for implementation. It’s fairly easy.

    Here’s an example to include the new gtag.js. Put this code snippet in your site’s header and replace UA-XXXXXXXX-X with your own tracking ID :

    
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-XXXXXXXX-X');
    </script>
    

    You can then include the previous code snippet I provided in a js (jQuery) file to register pageviews with the Auto Load Next Post plugin.

    • This reply was modified 6 years, 9 months ago by janvitos.
    • This reply was modified 6 years, 9 months ago by janvitos.
    • This reply was modified 6 years, 9 months ago by janvitos.
    • This reply was modified 6 years, 9 months ago by janvitos.
    Plugin Author Sébastien Dumont

    (@sebd86)

    I see. I just want to make sure that if I were to support this new method of tracking that the JavaScript is not loaded again should the user already have it installed.

    That is why the current GA integration I have provided requires that you have GA installed first for it to work.

    Thread Starter janvitos

    (@janvitos)

    You could do it the same way. The main difference though is that gtag.js requires passing the tracking ID ( UA-XXXXXXXX-X ) to record Pageviews, but I think you don’t need to pass it for the classic analytics.js.

    Thread Starter janvitos

    (@janvitos)

    Closed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add support for gtag.js ( Google Analytics )’ is closed to new replies.