Sending a Custom Metric with a Pageview Tag with GTM

One would think it is easy to send a Custom Metric with a pageview yet apparently there are some mishaps which may occur due to something obscure as a timing hit. Reading through the recent article written by Yeshoua Coren describing custom and calculated metrics there was a mention of this being the issue and the idea on how to solve it though not in detail (hitCallback) – fortunately enough I have only sent Custom metrics with events which by default do not add an additional timing hit so this article is just curiosity based:).

The issue

The real issue is when you send a pageview hit which has the set custom metric if there is a subsequent timing hit the custom metric will be inflated or better say sent twice. The volume of this additional hits is very well described on Google official developer site.  So if by any chance you opt to set Fields to set field siteSpeedSampleRate like this – your reports may be polluted:

siteSpeedSampleRate-field

So when you send a pageview this will happen (from debug):

set-custom-metric

GTM uses set on a named tracker – tracker name is in this case gtm1453223458541 so for each subsequent hit (while it persists) for this tracker name the Custom metric will be sent again. Fortunately a subsequent event will not be affected as the name of the tracker will changed as it uses timestamp in naming convention. And now the timing hit which follows immediately after the pageview hit:

timing-hit-with-custom-metric

The solution (so far)

In your Pageview tag add Fields to set Field > hitCallback:

hitCallback

The value will be a Custom  JavaScript variable (name it as you see fit) (note that with named trackers – when you explicitly define the tracker name the situation is ‘a bit different’):

function() { 
   return function() { 
      ga(function() { 
         tracker = ga.getAll()[0].get('name');
         ga(tracker+'.set','metric2', 0);
      }); 
   } 
}

Once you set this up the debug will show:

Running command: ga(“gtm1453224328813.set”, “metric2”, 0) which will set the value to 0 for the subsequent timing hit.

Considerations

Please note that this applies to simple implementations only and should be revised especially in cases where:

  • more trackers are present
  • you use named trackers

Would be extremely happy to hear if there are any other workarounds for the issue!:)

Leave a Reply

Your email address will not be published. Required fields are marked *