====== 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*). - register a //Custom Metric// in the Google Analytics back-end, to log your events //duration//: - enter into your Google Analytics web front-end; - in the Google Analytics //Property// settings, select //Custom Definitions// and then //Custom Metrics//; - select //New Custom Metric//; - create a //Custom Metric// with: * //Scope//: //Hit//; * //Formatting Type//: //Time// * (in this example we set the //Name// to //duration//)\\ {{ :ue5-google-analytics:editcustommetric-duration.png |}} - **important** take note of the Custom Metric //index// (it's 1-based), from the //Example Codes for This Metric// box; - in your application code, log the //duration// metric for the desired events: - calculate yourself the event duration (measured in seconds, as a floating point number); - add the calculated duration to a logged event (e.g. using ''Record Structured Event''): - add it as an event //Attribute//; - **important** as key use the string ''cm'', where must be substituted with the Custom Metric //index// discussed above. For example: ''cm1''.\\ {{ :ue5-google-analytics:recordstructuredevent-custommetric.png |}} 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//. - create a custom //Calculated Metric//: - in your Google Analytics Property //View// settings, select //Calculated Metrics// - select //New Calculated Metric//; - set the formula to: ''%%{{duration}} / {{Total Events}}%%'' (assuming your Custom Metric is called ''duration'')\\ {{ :ue5-google-analytics:editcalculatedmetric-duration.png |}} 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). {{ :ue5-google-analytics:customreportsetup-duration.png |}} This is an example of the corresponding output: {{ :ue5-google-analytics:customreportresult-duration.png |}} 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//.