UNAmedia Docs

Documentation & Knowledge Base

User Tools

Site Tools


ue5-google-analytics:universal-event-duration

Measuring Events Duration

This guide shown how to log the *duration* of an event and let Google Analytics to report aggregated statistics for it (like the *average duration*).

  1. register a Custom Metric in the Google Analytics back-end, to log your events duration:
    1. enter into your Google Analytics web front-end;
    2. in the Google Analytics Property settings, select Custom Definitions and then Custom Metrics;
    3. select New Custom Metric;
    4. create a Custom Metric with:
      • Scope: Hit;
      • Formatting Type: Time
      • (in this example we set the Name to duration)
    5. important take note of the Custom Metric index (it's 1-based), from the Example Codes for This Metric box;
  2. in your application code, log the duration metric for the desired events:
    1. calculate yourself the event duration (measured in seconds, as a floating point number);
    2. add the calculated duration to a logged event (e.g. using Record Structured Event):
      1. add it as an event Attribute;
      2. important as key use the string cm<index>, where <index> must be substituted with the Custom Metric index discussed above. For example: cm1.

GA will now be able to receive and sum all the durations you sent. If you don't see it, double check to pass the Custom Metric key as described above.

Aggregated statistics

We'll now instructs GA to calculate an aggregated statistics, the Average Duration.

  1. create a custom Calculated Metric:
    1. in your Google Analytics Property View settings, select Calculated Metrics
    2. select New Calculated Metric;
    3. set the formula to: {{duration}} / {{Total Events}} (assuming your Custom Metric is called duration)

This will allow GA to calculate the average duration for a group of events. The sum of duration and Total Events will be computed on the specific grouping of data displayed by Google Analytics.

Custom Report

You can now use both the Custom Metric and the Aggregated Statistic in your reports.

The example below is for a Custom Report where data are filter for a specific Event Action name, with resulting events grouped by the Event Label string, with outputs showing the calculate custom Average Duration (in addition to the associated Total Events and duration inputs used by GA to compute the formula).

This is an example of the corresponding output:

GA collects all the events satisfying the filter, group them accordingly to our configuration and for each resulting group it sums matching logged duration values and then compute the Average Duration.

ue5-google-analytics/universal-event-duration.txt · Last modified: 2022/09/01 12:48 by Staff