WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

How To Send Google Analytics Event with PHP

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?

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital related. His main expertise is with WordPress, Magento, Shopify as well as many other frameworks. Whether you need responsive design, SEO, speed optimisation or anything else in the world of digital then get in touch. If you would like to work with Nathan, simply drop him an email at [email protected]

It’s good to share

Join the discussion

Related Posts

jQuery / 20th September 2020

Detect Scroll up and Down event with jQuery

Read More