Particle Google Analytics WebHook series
[wp_ad_camp_3]
Welcome to my Google Analytics Particle webhook tutorial series. As you know from a number of my past tutorial series I like to keep things short. I would rather stick to one topic in each tutorial so that you get the full idea of what we are trying to achieve. You are more then welcome to skip around in the series if I mention something that was covered in another tutorial I will link you to that so that you can find clarification if you need.
In the first tutorial I will give you a little introduction into Google Analytics and my opinion on how it can be used with Internet of things devices (IoT) . In the second tutorial we look how the measurement protocol is used to send data to Google Analytics and I explained about the parameters that are required to send data to Google Analytics. In the third tutorial we looked at how to send Events to Google Analytics using Particle webhooks, and in the final tutorial we will look at sending Screen Views to an application Google Analytics account.
Particle Google Analytics WebHook series:
- Particle Google Analytics webhook – Introduction
- Particle Google Analytics webhook – Measurement Protocol
- Particle Google Analytics Webhooks – events
- Particle Google Analytics Webhooks – ScreenView
Particle Google Analytics Webhooks – events
This is is post number three in my Particle Google Analytics Webhook Tutorial series. In this tutorial I am going to show you how to create an event webhook and how to send the data to Google Analytics.
What are events?
Events are when something out of the ordinary happens and you would like to record that. User clicks a button. Light level goes up more then it should. We can record events to Google analytics.
There are four parameters you can send associated with events only two of them are required though
- Event Category: ec=Buttons // required Specifies the event category.
- Event Action: ea=Pressed // Required Specifies the event action.
- Event Label: el=LivingRoom // Specifies the event label
- Event Value: ev=1 // Specifies the event value. Values must be non-negative.
Create Webhook
While it is possible to create a web hook using the web interface i found it easier to do this command line
Place the following code into a file and run the following comandline
Particle webhook create eventHook.json
You should now have a new webhook created. Remember AccountId is the id of your account in the Google Analytics website.
[wp_ad_camp_5]
IP
When Google Analytics records a hit it gets the location from the location it was sent from. Webhooks are like little bots run by Particle. By sending uip we should be telling Google Analytics that the event is coming from us instead of particles servers. I had this working before but for some reason it doesn’t detect that i am sending events from Denmark in real time however the standard reports do detect that its Denmark. I will work on this and see if i can track down the reason why it is no longer working as i would expect.
Code
As you can see I am just building up a jSon string with the values i want the WebHook to send. CM1 is my custom metric number 1. This is just something simple i put together to show you how to do it.
You could just as easily send door open or door closed. Again its totally up to you what you are logging here. I have this set up in my kitchen recording light levels. Then in the event the light level changes by more then 50 from the last detection I send a light change event. The main reason for this was to detect when the automatic lights outside are activated. We have a cat that likes to sleep on the car and were wondering how often it does this. Answer is not that often or its not triggering the light. But I am detecting midnight refrigerator raids.
Remember there are some parameters that you must send in order to send the hit. Check the previous tutorial Particle Google Analytics webhook – Measurement Protocol before you start to remove some of the parameters I am sending here.
Checking
To see if your events are being recorded you can go to the Real-time report under the events you they should show up there as soon as you send them.
Conclusion
Creating a Google Analytics event webhook like I have done will enable you to send Google Analytics events from any of your particle devices. We are sending the account from the device so the webhook will work with any Google analytics account.
Hi Linda, thank you for the great blog!
I am trying to find out if i can configure google analytics to notify my application by webhook when certain event arrives.
So i will place a listener.php at
http://www.myserver.com/listener.php
and configure google analytics to notify this listener by http request each time a certain event happens.
Are you familiar with such feature?
Thank you!
Google analytics doesn’t have a watch feature like that currently. You could probably create something on your own by reading from the real-time api but you are going to have to check like every minute or something which may blow out your quota.