Analysis Workspace Hacks (AGE) – Metric Targets

This is a post in the Adam-Greco-Edition (AGE) series of posts. They aim to iterate on some great posts by Adam Greco, showing some different approaches to achieve similar things.

In another great Post, Adam Greco showed how we can have Metric Targets in Analysis Workspace. His approach includes setting up a Data Source to import Goals to a Custom Event.

This is a very nice approach, but has some serious limitations. Because it utilizes Data Sources, all their limitations apply (see documentation). Most importantly, data can not be deleted or changed once it has been imported. Also we need to sacrifice Custom Events for every Goal we set. The setup is also very involved and not suited for non-techie people.

What I would like to have is a Goal Metric that does not use valuable Custom Events, is changeable over time, and understandable and usable by non-technical users. As a plus, it would be nice if we could change it via both API and frontend. Maybe we can use a Calculated Metric for that?

Calculated Goal Metric

First, we need to tell Analytics if it should output our Goal in the Calculated Metric. To do this, we will be using the IF-function in the Calculated Metric Builder:

First the easy part: To tell Analytics what our Goal should be, put the Target value in the value_if_true box via a Static Number. Then, put a Static Number with “0” in the value_if_false box like this:

To keep it simple, we want to define a monthly Goal. The next step is to drop the “Month” Dimension in the logical_test box. This will open an overlay to select the month that we want to set for our Goal. This adds an Ad Hoc Segment for that specific Month, January 2019 in my case. In the Segment Container, drop any non-zero Metric like Occurences or Unique Visitors:

So, what this does is check if the current Month is the one we selected. If it is, it outputs our Goal, or 0 if not. Now, we just repeat this for all Months we want to cover and connect the functions with the Addition operator:

If i now use this new Metric for 2019 with the Month Dimension in Workspace, it gives me a nice Monthly Goal Metric and sums up all the values as Total:

Done! Whenever we want to change our Goals, we just have to edit our Metric. We could use it further to calculate the deviation from our Goal in Workspace. But there is still more…

Automate all the things

Adobe Analytics offers a huge set of APIs to change and automate almost everything. For our case, it would be nice to be able to update our Goals via an API. So let’s head to the Swagger UI to play around with that and authenticate on it!

First, we need the ID of our Calculated Metric. Open our newly created Goal Metric via the Components Menu in Analytics. The last Section of the URL includes the ID, starting with “cm”, like “components/calculatedMetrics/edit/cm2330_5e25a21g0e2a245s2147d09e“. In Swagger, extend the calculatedmetrics-section and the GET “/calculatedmetrics/{id}” function.

Click the Try-out button and enter the ID of your metric in the text box. In the expansion selector, make sure the “definition” part is selected. Once you click Execute, it gives you the request and response for that function. My response looks like below (without the IDs), where we can see all the Goals I defined (compare with the Screenshot above). If we wanted to change a Goal, we could update a part like “then”: 20000, to “then”: 22000, or whatever we like, and use the PUT “/calculatedmetrics/{id}” function to update the Goal. Amazing!

{
  "id": "cm2330_5e25a21g0e2a245s2147d09e",
  "name": "Goal Test",
  "description": "",
  "rsid": "id",
  "owner": {
    "id": id
  },
  "polarity": "positive",
  "precision": 0,
  "type": "decimal",
  "definition": {
    "formula": {
      "func": "add",
      "col1": {
        "func": "add",
        "col1": {
          "func": "add",
          "col1": {
            "func": "add",
            "col1": {
              "func": "add",
              "col1": {
                "func": "add",
                "col1": {
                  "func": "add",
                  "col1": {
                    "func": "add",
                    "col1": {
                      "func": "add",
                      "col1": {
                        "func": "add",
                        "col1": {
                          "func": "add",
                          "col1": {
                            "func": "add",
                            "col1": {
                              "func": "if",
                              "description": "If",
                              "cond": {
                                "func": "calc-metric",
                                "formula": {
                                  "func": "metric",
                                  "name": "metrics/visitors",
                                  "description": "Unique Visitors"
                                },
                                "version": [
                                  1,
                                  0,
                                  0
                                ],
                                "filters": [
                                  {
                                    "func": "segment-ref",
                                    "description": "Month = Jan 2019 (Jan 01-Jan 31)",
                                    "id": "id"
                                  }
                                ]
                              },
                              "then": 20000,
                              "else": 0
                            },
                            "col2": {
                              "func": "if",
                              "description": "If",
                              "cond": {
                                "func": "calc-metric",
                                "formula": {
                                  "func": "metric",
                                  "name": "metrics/visitors",
                                  "description": "Unique Visitors"
                                },
                                "version": [
                                  1,
                                  0,
                                  0
                                ],
                                "filters": [
                                  {
                                    "func": "segment-ref",
                                    "description": "Month = Feb 2019 (Feb 01-Feb 28)",
                                    "id": "id"
                                  }
                                ]
                              },
                              "then": 20001,
                              "else": 0
                            }
                          },
                          "col2": {
                            "func": "if",
                            "description": "If",
                            "cond": {
                              "func": "calc-metric",
                              "formula": {
                                "func": "metric",
                                "name": "metrics/visitors",
                                "description": "Unique Visitors"
                              },
                              "version": [
                                1,
                                0,
                                0
                              ],
                              "filters": [
                                {
                                  "func": "segment-ref",
                                  "description": "Month = Mar 2019 (Mar 01-Mar 31)",
                                  "id": "id"
                                }
                              ]
                            },
                            "then": 20002,
                            "else": 0
                          }
                        },
                        "col2": {
                          "func": "if",
                          "description": "If",
                          "cond": {
                            "func": "calc-metric",
                            "formula": {
                              "func": "metric",
                              "name": "metrics/visitors",
                              "description": "Unique Visitors"
                            },
                            "version": [
                              1,
                              0,
                              0
                            ],
                            "filters": [
                              {
                                "func": "segment-ref",
                                "description": "Month = Apr 2019 (Apr 01-Apr 30)",
                                "id": "id"
                              }
                            ]
                          },
                          "then": 20003,
                          "else": 0
                        }
                      },
                      "col2": {
                        "func": "if",
                        "description": "If",
                        "cond": {
                          "func": "calc-metric",
                          "formula": {
                            "func": "metric",
                            "name": "metrics/visitors",
                            "description": "Unique Visitors"
                          },
                          "version": [
                            1,
                            0,
                            0
                          ],
                          "filters": [
                            {
                              "func": "segment-ref",
                              "description": "Month = May 2019 (May 01-May 31)",
                              "id": "id"
                            }
                          ]
                        },
                        "then": 20004,
                        "else": 0
                      }
                    },
                    "col2": {
                      "func": "if",
                      "description": "If",
                      "cond": {
                        "func": "calc-metric",
                        "formula": {
                          "func": "metric",
                          "name": "metrics/visitors",
                          "description": "Unique Visitors"
                        },
                        "version": [
                          1,
                          0,
                          0
                        ],
                        "filters": [
                          {
                            "func": "segment-ref",
                            "description": "Month = Jun 2019 (Jun 01-Jun 30)",
                            "id": "id"
                          }
                        ]
                      },
                      "then": 20005,
                      "else": 0
                    }
                  },
                  "col2": {
                    "func": "if",
                    "description": "If",
                    "cond": {
                      "func": "calc-metric",
                      "formula": {
                        "func": "metric",
                        "name": "metrics/visitors",
                        "description": "Unique Visitors"
                      },
                      "version": [
                        1,
                        0,
                        0
                      ],
                      "filters": [
                        {
                          "func": "segment-ref",
                          "description": "Month = Jul 2019 (Jul 01-Jul 31)",
                          "id": "id"
                        }
                      ]
                    },
                    "then": 20006,
                    "else": 0
                  }
                },
                "col2": {
                  "func": "if",
                  "description": "If",
                  "cond": {
                    "func": "calc-metric",
                    "formula": {
                      "func": "metric",
                      "name": "metrics/visitors",
                      "description": "Unique Visitors"
                    },
                    "version": [
                      1,
                      0,
                      0
                    ],
                    "filters": [
                      {
                        "func": "segment-ref",
                        "description": "Month = Aug 2019 (Aug 01-Aug 31)",
                        "id": "id"
                      }
                    ]
                  },
                  "then": 20007,
                  "else": 0
                }
              },
              "col2": {
                "func": "if",
                "description": "If",
                "cond": {
                  "func": "calc-metric",
                  "formula": {
                    "func": "metric",
                    "name": "metrics/visitors",
                    "description": "Unique Visitors"
                  },
                  "version": [
                    1,
                    0,
                    0
                  ],
                  "filters": [
                    {
                      "func": "segment-ref",
                      "description": "Month = Sep 2019 (Sep 01-Sep 30)",
                      "id": "id"
                    }
                  ]
                },
                "then": 20008,
                "else": 0
              }
            },
            "col2": {
              "func": "if",
              "description": "If",
              "cond": {
                "func": "calc-metric",
                "formula": {
                  "func": "metric",
                  "name": "metrics/visitors",
                  "description": "Unique Visitors"
                },
                "version": [
                  1,
                  0,
                  0
                ],
                "filters": [
                  {
                    "func": "segment-ref",
                    "description": "Month = Oct 2019 (Oct 01-Oct 31)",
                    "id": "id"
                  }
                ]
              },
              "then": 20009,
              "else": 0
            }
          },
          "col2": {
            "func": "if",
            "description": "If",
            "cond": {
              "func": "calc-metric",
              "formula": {
                "func": "metric",
                "name": "metrics/visitors",
                "description": "Unique Visitors"
              },
              "version": [
                1,
                0,
                0
              ],
              "filters": [
                {
                  "func": "segment-ref",
                  "description": "Month = Nov 2019 (Nov 01-Nov 30)",
                  "id": "id"
                }
              ]
            },
            "then": 20010,
            "else": 0
          }
        },
        "col2": {
          "func": "if",
          "description": "If",
          "cond": {
            "func": "calc-metric",
            "formula": {
              "func": "metric",
              "name": "metrics/visitors",
              "description": "Unique Visitors"
            },
            "version": [
              1,
              0,
              0
            ],
            "filters": [
              {
                "func": "segment-ref",
                "description": "Month = Dec 2019 (Dec 01-Dec 31)",
                "id": "id"
              }
            ]
          },
          "then": 20011,
          "else": 0
        }
      },
      "col2": {
        "func": "if",
        "description": "If",
        "cond": {
          "func": "calc-metric",
          "formula": {
            "func": "metric",
            "name": "metrics/visitors",
            "description": "Unique Visitors"
          },
          "version": [
            1,
            0,
            0
          ],
          "filters": [
            {
              "func": "segment-ref",
              "description": "Month = Jan 2020 (Jan 01-Jan 31)",
              "id": "id"
            }
          ]
        },
        "then": 20012,
        "else": 0
      }
    },
    "func": "calc-metric",
    "version": [
      1,
      0,
      0
    ]
  }
}

Steps to create Goal Metric in Analysis Workspace:

  1. Create new Calculated Goal Metric

    Open the Calculated Metric Builder and drop as many IF functions as you would like for your Goal.

  2. Define Month Dimensions for each Function

    Select the Month that we want our Goal to be valid for.

  3. Set Goals in true-section and 0 in false-section

    In the value_if_true-section, we need to define our Goal and set the other Container to 0.

  4. Use Metric in Workspace

    Drop the Metric in a Freeform Table with the Month Dimension to see our Monthly Goals.

  5. Use API to update Metric

    If needed, use the Analytics API to update the Goals you defined before.


Avatar

Frederik Werner

Data Scientist roaming the realms of Web Analytics and Online Marketing Tech