Use case 4 (GA 4) – add and set a new test

The creation of a test based on Google Analytics 4 data is shown on a test containing simple check of sessions evolution. The sessions made by users over the past month are measured and the weekly average over the last month is compared to the amount of session made during the latest week. The share of sessions should be within the expected range unless there has been some expected event (e.g. state holiday which may cause decrease in online visits).

Follow the individual steps to proceed with creation of a test are described. If you need an overview how to create a dataset, where to find the "+ ADD TEST" button or where to locate the Query logic and Test logic, refer to the previous subsections.

  1. Go to Depots/[MyDepotName]/[MyGA4DatasetName] or create a new GA 4 Data dataset.

  2. Select button "+ ADD TEST".

  3. Set name to "Session change 7 days to 28 days", test code to "W4020", total score to 150 and do not select any checkbox (the template for the testLogic are not similar enough so this description is creating the whole test from the beginning).

  4. Change the dateRange parameter to have "startDate" value as "28daysAgo".

  5. Copy the object inside the dateRange parameter, insert it as another element in the array and replace the second "startDate" as "7daysAgo". The dateRage parameter now looks like this:

"dateRanges": [
        {
            "startDate": "28daysAgo",
            "endDate": "yesterday"
        },
        {
            "startDate": "7daysAgo",
            "endDate": "yesterday"
        }
    ]
  1. Delete the second object inside the "metrics" parameter to keep there only sessions.
  2. Delete the whole "dimensions" parameter
  3. In the "metricAggregations" you can delete the "MAXIMUM" and "MINIMUM" values and keep only "TOTAL" there. The whole queryLogic now looks like this:
{
    "dateRanges": [
        {
            "startDate": "28daysAgo",
            "endDate": "yesterday"
        },
        {
            "startDate": "7daysAgo",
            "endDate": "yesterday"
        }
    ],
    "metrics": [
        {
            "name": "sessions"
        }
    ],
    "metricAggregations": [
        "TOTAL"
    ]
}
  1. Insert parameters lowerRatioThreshold and upperRatioThreshold to specify the allowed share of sessions between last week and weekly average of the last month. The following values allow the share to vary between 70 % and 140 % (i.e. the amount of sessions last week can be up to 30 % lower or up to 40 % higher than the last month's weekly average).
const lowerRatioThreshold = 0.7;
const upperRatioThreshold = 1.4;
  1. Transform the input data to get the summary of the sessions (the second parameter in the function specifies which dateRange value the results correspond to):
const session28dSummary = waaila.functions.summarizeGaResult(results,0);
const session7dSummary = waaila.functions.summarizeGaResult(results,1);
  1. Extract the total value from the summary and calculate the weekly average for the last month:
const sessions28d = session28dSummary['sessions']['total'];
const sessions7d = session7dSummary['sessions']['total'];
const sessions7avg = sessions28d / 4;
  1. Next, define pass and fail messages to be displayed in case of pass or failure. The parameters lowerRatioThreshold and upperRatioThreshold are passed into the messages for better clarity.
const assert_pass_message = `Sessions this week do not differ significantly from last week (they are between ${lowerRatioThreshold*100} and ${upperRatioThreshold*100} % of average sessions for a week)`;
const assert_fail_message = `Sessions this week are not between ${lowerRatioThreshold*100} and ${upperRatioThreshold*100} % of average sessions for a week`;
  1. Lastly, the assertion is constructed to check the condition that share of sessions last week to weekly average of sessions last month is between the two parameters. The score is set to 150 (in case of passing the condition). Pass and fail messages are passed in respective cases.

    The whole test logic now looks like this:

(results, waaila) => {
    const lowerRatioThreshold = 0.7;
    const upperRatioThreshold = 1.4;
    
    const session28dSummary = waaila.functions.summarizeGaResult(results,0);
    const session7dSummary = waaila.functions.summarizeGaResult(results,1);
    const sessions28d = session28dSummary['sessions']['total'];
    const sessions7d = session7dSummary['sessions']['total'];
    const sessions7avg = sessions28d / 4;
    
    const assert_pass_message = `Sessions this week do not differ significantly from last week (they are between ${lowerRatioThreshold*100} and ${upperRatioThreshold*100} % of average sessions for a week)`;
    const assert_fail_message = `Sessions this week are not between ${lowerRatioThreshold*100} and ${upperRatioThreshold*100} % of average sessions for a week`;
    waaila.assert((sessions7d / sessions7avg > lowerRatioThreshold) && (sessions7d / sessions7avg < upperRatioThreshold), 150)
        .pass.message(assert_pass_message)
        .fail.message(assert_fail_message);
}