Installing counter on AJAX-, SPA-, Flash sites

To make counter work properly you should do several steps:

  1. Get and install the counter code on the site as described in other help articles.

  2. Identify the cases and events needed to be treated as a separate page view or goal achievement.

  3. You also need to define url and referer for this view. By default will be used default url and referer from location.href and document.referrer.

  4. Insert additional code into places where a new view is initiated. You should call pageView or reachGoal method for _tmr object to send new pageview or target reach respectively.

Calling a method for AJAX sites (you need to take only one option depending on the sending url or referer):

_tmr.pageView({ id: "counter ID" });

or with url

_tmr.pageView({ id: "counter ID", url: "URL" });

or with url and referrer

_tmr.pageView({ id: "counter ID", url: "URL", referrer: "REFERRER" });

If you are not sure is this counter code already inserted on the page and loaded, you can use a safer version of pageview sending:

var _tmr = window._tmr || (window._tmr = []);
_tmr.push({ id: "36383", type: "pageView", url: "URL" });

For SPA sites you can often find a method in which you can insert code similar to the code for AJAX sites. For example in AngularJS you can use the $viewContentLoaded or $routeChangeSuccess

function MyCtrl($scope, $location, $window) {
  $scope.$on('$viewContentLoaded', function(event) {
    $window._tmr.push({id:"counter ID", type: "pageview", url: $location.url() });
  });
}

or

app.run(function ($rootScope, $location) {
    $rootScope.$on('$routeChangeSuccess', function(){
        _tmr.push({id:"counter ID", type: "pageview", url: $location.url() });
    });
});

If you use GTM to insert the code and your AJAX, SPA site changes the URL of the page on new view, then instead of inserting the above code you can specify the “Change in History” trigger in addition to the “Page View” trigger.

Calling a method for Flash sites with ActionScript2 (Flash version is less than 9.0):

getURL("javascript:_tmr.pageView({id: \"counter ID\", url: \"URL\" })");

Calling a method for Flash sites with ActionScript3 (Flash 9.0 or later):

navigateToURL(new URLRequest("javascript:_tmr.pageView({id: \"counter ID\", url: \"URL\" })"), "_self");