Sending sensors Data to Google Analytics trough measurement protocol

Although I’m not a developer, I’ve been playing around with Arduino and other microcontrollers for some years now and this is a project that I’d like to share because I had the first idea some years ago.

The end result will look like this:


But first:

If you don’t know what measurement protocol is, here’s a brief description: It’s a protocol that allows you to send data to Google Analytics by using HTTP requests.

If you don’t know what ESP8266 is, here are some details: The ESP8266 is a low-cost Wi-Fi microchip with full TCP/IP stack and microcontroller capability.

Long story short:

I’ll try to log the humidity in the room using a DHT22 Sensor, and a ESP8266 microcontroller (packed as a WEMOS D1 mini pro) in Google Analytics.

Let’s begin:


The wiring is simple and can be seen in the following images:


Note: In my achievement photo is a battery module because I wanted to go fully wireless, but that is not needed for the project to work.

Hit Builder:

This is a tool that can help you build the hit type and parameters and I recommend spending some time into hit design. You might want to analyze your data afterwards and based on the initial design you can get different results. E.g: averages, segments.

For simplicity I used events and values and the result looks like this: v=1&t=event&tid=UA-53504721-1&cid=3d31d389-603a-4c88-837b-f5c009c9d05b&ec=Umiditate&ea=”+humidityS+”&el=Label&ev=”+humidityS

Here you have the link and docs:


Projet d'apprentissage d'un objet connecté (IoT) pour réaliser une sonde de température
ESP8266 + DHT22 + LED + MQTT + Home-Assistant
Projets DIY ( - Mai 2016
Licence : MIT
#include <ESP8266WiFi.h>
#include "DHT.h" // Librairie des capteurs DHT
#include <ESP8266HTTPClient.h>

#define wifi_ssid “wifinetwork”
#define wifi_password “wifipass”

long lastMsg = 0;
long lastRecu = 0;
bool debug = true; //Display log message if True

#define DHTPIN D4 // DHT Pin connected to the sensor

// Un-comment you sensor
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)

// Create abjects
WiFiClient espClient;

void setup() {
pinMode(D2,OUTPUT); //Pin 2 for LED
setup_wifi(); //Connect to Wifi network

//WiFi Connection
void setup_wifi() {
Serial.print(“Connecting to “);

WiFi.begin(wifi_ssid, wifi_password);

while (WiFi.status() != WL_CONNECTED) {

Serial.println(“WiFi OK “);
Serial.print(“=> ESP8266 IP address: “);


void loop() {
HTTPClient http;
String temperatureS;
String humidityS;

long now = millis();
// Send a message every minute
if (now – lastMsg > 1000 * 60) {
lastMsg = now;
// Read humidity
float h = dht.readHumidity();
// Read temperature in Celcius
float t = dht.readTemperature();

// Oh, nothing to send
if ( isnan(t) || isnan(h)) {
Serial.println(“KO, Please chez DHT sensor !”);

if ( debug ) {
Serial.print(“Temperature : “);
Serial.print(” | Humidity : “);

temperatureS= String(int(t));
humidityS = String(int(h));
http.begin(“”); //HTTP
http.addHeader(“POST”, “/collect HTTP/1.1”);
http.addHeader(“Host:”, “”);
// Serial.println(“Request made to:”+temperatureS+”&el=Label&ev=”+temperatureS);
// http.POST(“v=1&t=event&tid=UA-53504721-1&cid=3d31d389-603a-4c88-837b-f5c009c9d05b&ec=Temperature&ea=”+temperatureS+”&el=Label&ev=”+temperatureS);
int httpCode = http.GET();

Note: The code is copy pasted and modified for my needs, but one thing worth mentioned is that by default you will get HTTPS://Collect. For some reason, I suspect the insufficient memory on the ESP, it won’t work. Instead use HTTP and Google will take care of the redirect.


Long story long:

The initial idea to build something similar hit me when GA first introduced measurement protocol and it was to build a proof of concept offline store GA tracker.

The store would have two sensors that will help me measure the people coming in by monitoring the laser beams order interruptions:

  • 2,1 would send a new session in GA
  • 1,2 would send a session end.

And the second part of the measurement was based on a pressure sensor under my books/products pile that will help me measure the products. Based on the initial number of books and pressure, I could estimate the average pressure per book and:

  • Everytime the pressures drops, I can send a product view;
  • Everytime the pressure is down for a specified amount of time, I can send an add to cart event;
  • Everytime I have a session ended and the pressure is down I can send a transaction.
  • Everytime I have the pressure back up I would send a cancelled transaction.



Of course the design has flaws, but was enough to prove the power of the new feature back then. Since time is a limited resource that we can trade, I came back with this article years later just because I wanted to close this chapter.