In this guide, we build a slightly more sophisticated example of a reporting workflow to illustrate the capabilities of PushMetrics.
In practice, you might rather want to start with one of our templates but for the sake of this example, we will build everything from scratch.
In this example, we will
We assume you have a basic understanding of PushMetrics from the first getting started guide.
Just like in the first example, we start by creating a blank new Notebook:
In PushMetrics you can write SQL queries against Google Sheets to both read and write data to it.
In this example, we make use of this to pull a list of users that will become the recipients of our alerts.
Here is the list:
Great, now we have the recipient list from the Google Sheet available in PushMetrics.
Next, we will query the KPIs we want to send out.
For this, we create a new SQL block like this:
And a second SQL block right below like this:
We don't want to get into the details of the SQL itself but notice this:
query_2
we are filtering for region = 'West'
. This is only temporary. We will make this dynamic and filter for the region of each particular recipient from the Google Sheet in the next step.query_3
we are selecting  from( {{ query_2 }} )
. Upon execution, the SQL from query_2 will be injected here. Of course, we could cram this into one long query but this illustrates how you can split up complex queries into separate blocks in PushMetrics. Right now, we are only generating the report data for region = 'West'
but what we actually want is to loop over the recipient list from query_1
and execute the report queries for each of the recipient's regions.
Luckily, this is quite easy in PushMetrics. All we need to do is to wrap the queries inside a FOR LOOP
:
{% for row in query_1.data %}
. This will access the data from the first query that returns us the recipient list from Google Sheets{% endfor %}
.query_2
we change the filter region = 'West'
to region = '{{ row.region }}'
, which will replace the hard-coded "West" with the correct region of each recipient.On execution, for every row in the recipient list, the queries inside the for loop
are executed and the region value is replaced dynamically.
Now that we have the data we need, and we loop over the recipient list, we can draft a nice email.
But before that, we do one small little tweak that will make our lives a bit easier:
{% set result = query_3.data[0] Â %}
.[0]
) of query_3
to a new variable called result
result.sales
instead of having to write query_3.data[0].sales
Now we are ready to craft our email message:
row
variable from our loop) and the final report query ( query_3
, now dubbed result
)....sales in your region {{ row.region }} changed in the week of {{ result.week }} by {{ result.pct_change }}...
the region information comes from the Google sheets data, the week, and the percentage change from the report query.Now, this version would already work pretty well as a simple burst report, where every recipient receives a personalized KPI report in their inbox.
However, we only want to send the email IF the change in Sales week over week is above a certain threshold.
For this, we just wrap the email inside an IF
statement that checks for our alerting condition:
for loop
before, we wrap our email inside an if
statementThe alert rule is quite simple: {% if result.pct_change|abs() > threshold %}
pct_change
from our report query result.abs()
, separated with a |
(after all, we want the alert to trigger for both positive and negative changes)> threshold
{% endif %}
So whenever the change in sales for a given recipient is greater than the parameter value (in our example 50%), the email message will be sent.
OK, it looks like we are almost done.
Let's give it a try and run the whole thing:
A few moments later, your inbox should look something like this:
This brings us to our final point: Scheduling this to be run automatically.
I hope this example was able to illustrate how you can build custom workflow logic in a PushMetrics notebook using a combination of:
Combining all these different building blocks allows for complex, almost limitless possibilities.
To learn more, check out the other sections of the documentation and get started building.