A different take on Google Analytics: monitoring IoT devices

Google Analytics is the original analytics software and has been part of Google since 2005. The new version Google Analytics 4, or GA4 for short, breaks new ground. Admittedly: It takes a bit of rethinking and getting used to, but I’m slowly coming to terms with it.

Monitoring of IoT devices – requirements

We have a handful of IoT devices in use. These send messages to an end platform via our Lorawan and Stack. In concrete terms:

A temperature sensor sends the temperature to a dashboard via Lorawan every 10 minutes. These are recorded there. Then there is another rain sensor, which sends the data to another platform every 15 minutes to archive it there.

Send any number of IoT sensors to any number of recipients.

The question now was:

  • How can I know how many devices are active daily/monthly and use the distributor?
  • Which end customers are actively used?

Why are these figures interesting?

There are three simple reasons for this:

  • The distributor is billed depending on the number of devices used.
  • Load planning. The architecture is such that it is theoretically self-scaling. But control is always better!
  • Very simple monitoring. Are there any sudden jumps or even gaps? These could indicate errors.

Solution approach

Since the entire stack is serverless and no easily accessible database is available, a simple counter cannot simply be added without further ado. An obvious approach would be to dock a database and write a small application: = a lot of effort.

And if you have your own solution, then it should probably be a time series database, because this in turn should actually record all events. So à la InfluxDB or even a ready-made tool with Grafana, where you can then make all kinds of evaluations.

BUT: Yet another new tool, which is associated with additional costs… when all I’m really looking for is a simple application, namely how many IoT devices connect via the distributor each month.

Hello world – Google Analytics

Then the brilliant idea: Google Analytics boasts that GA4 is no longer limited to websites, but to everything possible and impossible. In fact, I have a managed timeseries database here for free.

The GA4 creators probably didn’t think of this use case, but hey… That’s what computer science is all about.

And this is the solution

The solution is as simple as it is brilliant. A small snippet sends an event to Google Analytics for every message:

def send_to_ga4(frame, device):
    config = current_app.config
    url = f\"https://www.google-analytics.com/mp/collect?measurement_id={config[\'GA4_MEASUREMENT_ID\']}&api_secret={config[\'GA4_API_SECRET\']}\"
    hash_object = hashlib.sha256(frame[\"devid\"].encode(\'utf-8\'))
    client_id = hash_object.hexdigest()
    session_id = client_id + datetime.now().strftime(\"%d%m%y\")
    payload = {
        \"client_id\": client_id,
        \"non_personalized_ads\": False,
        \"events\": [
            {
                \"name\": \"uplink\",
                \"params\": {
                    \"session_id\": session_id,
                    \"engagement_time_msec\": \"100\",
                }
            }
        ]
    }
    for application in device[\'application\']:
        payload[\"events\"].append(
            {
                \"name\": \"page_view\",
                \"params\": {
                    # \"page_location\": application[\"url\"],
                    \"page_title\": application[\"name\"]
                }
            }
        )
    r = requests.post(url,data=json.dumps(payload),verify=True)

A few more explanations:

  • The client_id is the ID of the device
  • The session_id is the ID of the device, plus the current date. This means that a session lasts 1 day and I can count how many messages a device sends per session (day).
  • The pages_views are the recipients. This makes it clear which customers are needed and how much.

Voilà. And with this we have built a super simple monitoring tool. Even the real-time report works. And if you had coordinates, you could even locate the devices.

For once, we don’t have to worry about data protection, as only a few hashed IDs are transmitted.