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

In this use case we describe the whole process of creation of a new test using Google Analytics data on an example of a check of visits on single domain website. In a single domain website, most of the traffic shall be aligned to only one hostname. If the traffic splits among multiple hostnames, there is a potential of badly configured Google Analytics.

To verify that this is not a problem, you can extract data for the most visited hostname and check its share. In particular, you can first collect data on number of sessions per hostname and then extract the maximum number of sessions per a hostname and compare this value with the total number of sessions. You can evaluate the ratio of the maximum and total number of sessions against a selected threshold. For this test, we first set the threshold to 98 % - that means that up to 2 % of traffic coming from different hostnames is considered acceptable. You can evaluate the test with this threshold and if you need, adjust the threshold so that it suits your needs better. Ideally, you always receive the information that you are within the given threshold, which means that vast majority of sessions is aligned with the single hostname. However, if it fails you should check if there is some filter for inclusion of hostnames applied, if not, create one.

Below 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]/[MyGADatasetName] or create a new Google Analytics dataset
  2. Select button "+ ADD TEST".
  3. Set name to "Visits on single domain website", test code to "W0110" and select the Assertion template.
  4. In the Query logic set the startDate to "28daysAgo" : "startDate": "28daysAgo
  5. Reduce the metrics only to "ga:sessions" (exclude "ga:users" so that metrics looks as follows):
"metrics": [
    {
        "expression": "ga:sessions"
    }]
  1. Replace the dimensions with a single dimension, "ga:hostname" :
"dimensions": [
    {
        "name": "ga:hostname"
    }]
  1. Delete the line with filtersExpression
  2. Adjust the ordering to sort the data by sessions, i.e. replace fieldName from "ga:date" to "ga:sessions" and sortOrder from "ASCENDING" to "DESCENDING"
"orderBys": [
    {
        "fieldName": "ga:sessions",
        "sortOrder": "DESCENDING"
    }]

You should now have the following input in the Query logic:

{
  "requests": [
    {
      "id": "request1",
      "queries": [
        {
          "dateRanges": [
            {
              "startDate": "28daysAgo",
              "endDate": "yesterday"
            }
          ],
          "metrics": [
            {
              "expression": "ga:sessions"
            }
          ],
          "dimensions": [
            {
              "name": "ga:hostname"
            }
          ],
          "orderBys": [
            {
              "fieldName": "ga:hostname",
              "sortOrder": "DESCENDING"
            }
          ]
        }
      ]
    }
  ]
}
  1. Into the Test logic just below the definition of inputs (results, waaila) insert the following 2 lines to allow easy adjustment of the threshold constant sessionsRatio:
    // Test Configuration:
    const sessionsRatio = 0.98
  1. Delete the line defining totalUsers and below the extraction of data (sessions) and the definition of TotalSessions add lines extracting the maximum number of sessions per hostname and the respective hostname. Note, that you need to extract the maximum from the summarizedResult called sessionsSummary, while to access the respective hostname, you need to work with the normalizedresult saved under the name sessions.
    const maxSessions = + sessionsSummary['sessions']['maximums'];
    const hostName = sessions[0]['hostname'];
  1. Adjust the assert() function condition to maxSessions / totalSessions > sessionsRatio and change the assert message by the following expression. In case of failure, it should display the hostname along with the threshold in percentage points. You can leave the score at 80.
`The share of the most visited hostname (${hostName}) on all sessions is not higher than ${sessionsRatio * 100} %`

You can change the comment above assert which is describing the test from // Check that there are any sessions at all to // Check the share of largest hostname against the sessionsRatio.

At this point you should have the following content of the Test logic:

(results, waaila) => {
    // Test Configuration:
    const sessionsRatio = 0.98

    // Transform the data
    const sessions = waaila.functions.normalizeGaResult(results['request1'][0]);
    const sessionsSummary = waaila.functions.summarizeGaResult(results['request1'][0]);
    const totalSessions = +sessionsSummary['sessions']['total'];
    const maxSessions = + sessionsSummary['sessions']['maximums'];
    const hostName = sessions[0]['hostname'];

    // Check the share of largest hostname against the sessionsRatio
    waaila.assert(totalSessions > 0, 80).fail.message(`The share of the most visited hostname (${hostName}) on all sessions is not higher than ${sessionsRatio * 100} %`).break;

}

If you want to extend it to check for presence of the data (thus avoiding errors due to empty data even when using tiny Google Analytics accounts errors), you can add a warn() function to check for presence of data (check if sessions is not an empty Array). You want to end the run in case there would be no data, so there is .break behind the warn() function. After this addition, the Test logic looks like this:

(results, waaila) => {    
    // Test Configuration:
    const sessionsRatio = 0.98
    
    // Transform the data
    const sessions = waaila.functions.normalizeGaResult(results['request1'][0]);
    const sessionsSummary = waaila.functions.summarizeGaResult(results['request1'][0]);
    
    waaila.warn(typeof sessions[0] != 'undefined', 20)
        .fail.message('No data returned for sessions by hostname.').break;
    
    const totalSessions = + sessionsSummary['sessions']['total'];
    const maxSessions = + sessionsSummary['sessions']['maximum']
    const hostName = sessions[0]['hostname'];
    
    // Check the share of largest hostname against the sessionsRatio
    waaila.assert(totalSessions > 0, 80).fail.message(`The share of the most visited hostname (${hostName}) on all sessions is not higher than ${sessionsRatio * 100} %`).break;
}
  1. Run the test by clicking on the "Run" button above the Query logic
  2. If the test has failed due to the threshold 0.98 being too high for your case, consider setting it to a level more appropriate to your case or learn more about our available tests for testing hostnames
  3. Save the test by using the either the "Save" button on the right, or the "Save and run" from the "Run" button drop-down if you want to run the test again
  4. To exit the editor, navigate using the navigation line above the information about the test