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.



Unelte debugging Google Tag Manager

Daca in teorie GTM incearca sa faciliteze cursul Marketing <-> Developer, in realitate, pentru un developer ramane la fel de neimportanta lipsa unui tag de remarketing de pe site, ca si pentru un marketer procesul de implementare a unui tag (e doar un copy-paste).

Adevarul este undeva la Darwin, dar daca intr-un final tu vrei codul de remarketing pe site ASAP, va trebui sa ii explici developer-ului de unde sa dea copy si unde sa faca paste.

Pentru asta, in ordinea utilitatii marginale a.k.a random, am enumerat mai jos o serie de unelte pe care le folosesc in diverse momente ale implementarii GTM.

Google Tag Assistant

Este o extensie pentru Google Chrome care ofera informatii si sugestii de imbunatatire referitoare la tag-urile oferite de Google. Printre ele avem Google Analytics, AdWords Remarketing + Dinamic cu verificare in feed, Conversii AdWords, Google Tag Manager, Website Call Tracking, Floodlight..

Continue reading

[en] Facebook Login & Universal Analytics

There are two things I’d like to talk about in this article and these are:

1) How to use Facebook ID as the User ID in Google Universal Analytics and why to do that

2) How to create Facebook Custom Audiences based on Google Analytics data

[bonus tip at the end]

First of all, I’m not a developer and even though, I’ve managed to put in place a working experimental page that you can find here.

Let’s start:

Continue reading

[en] Facebook Tag API & Event Based Custom Audiences

First of all, english is not my native language so I’ll appreciate your feedback.

What is Facebook Tag API?

You can find the answer here, but, to be short, it gives you the power to create custom audiences, based on almost everything you can dream of. (not just URL)

How does it work?

It’s simple and the answer is also here. Instead of pushing the standard  code:

window._fbq.push(["track", "PixelInitialized", {}]);

now, you push your custom event:

_fbq.push(['track', eventName, customData]);

but in the real world it should look more like this

window._fbq.push(['track', 'emailClick', {}]);

The option to segment your audience based on events will become available only after the custom tag will be fired. So, place the custom tag on your website and wait.

Continue reading

Facebook Custom Audience – Event Based

Una dintre limitarile retargetingului in Facebook este faptul ca segmentarea audientei se face doar la nivel de URL.

Sau nu?

Facebook ne pune la dispozitie TAG API, functionalitate despre care nu am avut prea multe articole de citit pentru ca nu am gasit, dar pe care am testat-o si cred ca merita impartasita.  Pentru asta am selectat ca si exemplu: Retargetarea in Facebook a utilizatorilor in functie de cautarea facuta in Google (doar pentru utilizatorii care vin dintr-un anunt)


Inserarea dinamica a cuvantului cheie in url printr-un parametru specific. Eu am ales “keyword” 🙂 Atentie la alegerea parametrului deoarece acesta poate interactiona cu alti parametri sau suprascrie datele din Google Analytics.

Se modifica destination URL in campanii dupa cum urmeaza: devine{nespecificat}


Continue reading

Tag Manager, History Listener, Google Analytics, Virtual Pages

Zilele trecute am implementat Google Tag Manager pe HotSeat.Ro si m-am si jucat putin cu el. Printre altele mi-am propus sa monitorizez fiecare sectiune a paginii ca fiind o pagina separata in Google Analytics. Fiecare sectiune este marcata de un fragment identifier (textul aflat dupa caracterul# in bara de adresa) si poate fi folosit in general pentru a trimite exact pe sectiunea dorita din pagina (de exemplu:formularul de inscriere ).

Problema este ca in mod natural Google Analytics nu face asta, deoarece nu exista o reincarcare a codului pe o pagina cu un URL diferit.

Solutia, am gasit-o pe blogul unuia dintre autorii (omul asta chiar scrie epic despre Tag Manager) mei preferati Simo Ahava.

Mai exact, am folostit event-ul historyChange pentru a trimite pagini virtuale in Google Analytics.

Pasul 1:

Continue reading