Today we are going to show you a method of sending a Google Analytics Event with PHP. You might be asking yourself why you would want to do this but we came across a scenario where we wanted to send an event to Google Analytics on a WordPress user registration hook.
Sending Event using Javascript
Here is an example of how you would send a GA Event using JavaScript:
ga('send', {
hitType: 'event',
eventCategory: 'Registration',
eventAction: 'Submit',
eventLabel: 'New User Registration'
});
And here is how you would send the event using PHP
Yes, you can send the same event using PHP on the service-side by sending a POST request to Google Analytics API with the data of the event. We have used Curl to make the request as shown below:
// GA curl
$req = curl_init('https://www.google-analytics.com/collect');
curl_setopt_array($req, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS =>
'v=1&t=event&tid=UA-XXXXXXXX-X&cid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&ec=Console&ea=submit&el=Feedback%20Form%20Submission'
));
// Send the request
$response = curl_exec($req);
The overview of the POST requests
As we can see from the above post request, this is made up of 7 different parameters:
v=1&t=event&tid=UA-XXXXXXXX-X&cid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&ec=Console&ea=submit&el=Feedback%20Form%20Submission
To better understand what this means, see the table below:
Param Name | Meaning | Value |
---|---|---|
v | Version | 1 |
t | Type | event |
tid | Tracking ID | (Your Tracking ID) |
cid | Client ID | (Your GA Client ID) |
ec | Event Category | Registration |
ea | Event Action | Submit |
el | Event Label | New%20User%20Registration |
Note: The last 3 parameters (ec, ea and el) are the same ones in which you use in the JS code snippet above, however, we need to URL encode these values.
At this stage, you might be wondering how you can get the Client ID, no problem, we have you covered here too.
To get the Client ID, simply open up the website which has the Google Tracking code for Google Analytics, we did this with Google Chrome… Within the console, go to the ‘Application’ tab and click on ‘Cookies’ as shown below:
This tracking code value with become your cid
or Client ID.
Once you have implemented this, you can test to see if it is working by heading over to Google Analytics, selecting the option for ‘Realtime’ and then clicking on ‘Events’, if this has worked, you should see something as shown below:
Bonus Tip
Did you know you can use events to track goals?
Well, we did this for a specific client and we can create this in Google Analytics now by heading over to the ‘Admin’ area and then clicking on the ‘Goals’ link.
When creating the goal, we selected the ‘custom’ goal setup. Now, name your goal and then select the ‘Event’ radio option. Within the Goal Details, all we have to do is set the Category to the name we defined within ec
, which in our case was ‘Registration’.
Now we have a goal in place which will look as follows:
And there we have it!
Not only have we now passed an event in Google Analytics using PHP instead of JS, but we have now created a goal which is measured by the event. Pretty neat right?
Hi, I’ve been using your code for a while and it work very well but I’m having an issue with the goal parts. When I visit the goal overview tab in my analytics, it said that 90% of the goal completion url come from (entrance) or the server where my web site is hosted. Do you know how to fix this so the goal completion represents the url where the form was sumbited?