infinite scroll + masonry + google analytics

I know it’s been done a million times before, but here it is again:

// Masonry and Infinite Scroll
$(function(){

// define content container
var $container = $('#content');

// apply masonry to loaded elements
$container.imagesLoaded(function(){
  $container.masonry({
    itemSelector: '.post', // your posts
    isAnimated: true
  });
});

// apply infinite scrolling
$container.infinitescroll({
  navSelector  : '#page-nav',    // selector for the paged navigation
  nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
  itemSelector : '.post',     // selector for all items you'll retrieve
  extraScrollPx: 0,
  loading: {
      finishedMsg: 'No more items to load.',
      msgText: '',
      img: '.../ajax-loader.gif'
    }
  },
  // trigger Masonry and google analytics in the callback
  function( newElements, opts ) {
    // track google page view
    var page = opts.state.currPage;
    try { ga('send', 'pageview', '/page/'+page); } catch(err) {  }
 
    // masonry elements that have just loaded onto the page
    var $newElems = $( newElements ).css({ opacity: 0 });
    var $newElemsIDs = $newElems.map(function () { return this.id; }).get();
    $newElems.imagesLoaded(function(){
      $newElems.animate({ opacity: 1 });
      $container.masonry( 'appended', $newElems, true );
      // apply code here to do stuff after images loaded
    });
  });
});