3

Google Analytics, by just placing its sourcecode on my website, automatically tracks everything I used to need (pageviews, unique visitors).

But now, I need to track events, and the only way to do this is to do it server-side. Each time any users does an specific action i need to track, the server posts data to google to track the information, as explained here:

https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#event

And it does works amazingly perfect, but, since I realiced, I am now receiving a LOT of visits from Spain, doubling the visits from USA. And before I implemented the event tracking, Spain wasn't even part of the top 10 countries.

Today I have realiced that my servers are in Spain, and that may be causing the issue.

How can I track the event, without making it count as a pageview?

$url = 'http://www.google-analytics.com/collect';
$data = array('v' => '1', 'tid' => 'UA-HIDDEN-1', 'cid' => $_SERVER["REMOTE_ADDR"], 'ni' => '1', 't' => 'event', 'ec' => '', 'ea' => 'JUMP', 'el' => '');
$options = array(
 'http' => array(
 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
 'method' => 'POST',
 'content' => http_build_query($data),
 ),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

Thank you very much!!

Babblo
8036 silver badges18 bronze badges
asked Dec 9, 2013 at 3:35
4
  • I wanted the title to be 'Google Analytics server-side tracking'. I am sorry for the mistake. Commented Dec 9, 2013 at 3:36
  • Take a look at this: stackoverflow.com/q/9503329/722135 (Also, I fixed the title) Commented Dec 9, 2013 at 4:54
  • And this support.google.com/analytics/answer/1034840?rd=1 Commented Dec 9, 2013 at 4:55
  • I assume you have a reason for needing to track events from the server and not from the client as is normally done? I posted answer below. Commented Dec 9, 2013 at 6:08

2 Answers 2

1

You are sending the IP adress as a client id, which is wrong. For one, the client id is supposed to be an UUID. Secondly, Analytics won't recognize that these events belong to an existing user.

You'd need to grab the existing client id for an existing user on the web page:

ga(function(tracker) {
 var clientId = tracker.get('clientId');
});

and then send it back to the server and use it in your request (1). At the moment GA cannot assign correct geo information since the events do not belong to the session of the user who initiates the event (this quite possibly affects some other metrics, too).

(1) You might as well read the GA cookie in PHP, but Google recommends against it since the cookie format might change without notice. The script above will always return a correct client id even if the cookie format changes.

Updated: I have read a bit more documentation and while my answer seems still somewhat relevant it's probably wrong for the actual use case - Geo is determined by IP and the serverside script will still send the servers IP. So quite possibly (haven't done the science yet) this would look like one visitor with two devices instead of a single visitor.

Update 2: Apparently it is now possible to include the users IP adress as parameter, so this answer is possibly no longer relevant.

Here is a techopad presentation about mixing UA client- and serverside, maybe that helps.

answered Dec 9, 2013 at 8:53
Sign up to request clarification or add additional context in comments.

1 Comment

Well it seems my answer was a bit off - I included a bit more info I found. It would be great to know if you are making progress with this.
0

An event in and of itself is not a pageview. See: Event Tracking

Is there a specific reason why you need to track events server side and pageviews from the normal ga.js client-side code?

You can easily track events from the client side, if you were unaware of that: <a href="#" onClick="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);">Click Link to Track Event</a>

Assuming that you needed to keep events AND pageviews on the server side:

<?php
 //Put SERVER_ADDR into a var
 $request_ip = $_SERVER['REMOTE_ADDR'];
 // Put any server IPs you need to filter out below in an array
 $localhosts = array('127.0.0.1','192.168.15.1','10.1.10.1');
 // Use this later
 $url = 'http://www.google-analytics.com/collect';

Now, Figure out what to do with the REMOTE_ADDR check if its in our list above. then build an array of type to send GA (events, pageviews)

 $actions = array();
 // Note that the values are arbitrary and will let you do what you need.
 if(in_array($request_ip)){
 //Only track event, or track pageview differently, or track two events.
 $handle_myServer = true;
 $actions = ('event');
 } else {
 // Track everyone else
 $handle_myServer = false;
 $actions = ('event','pageview','mySpecialPageview','mySpecialEvent');
 }

Finally We have built a list of events we can use in flow control with existing code for pageviews, user timing, events, etc. Be creative!

 foreach($actions as $action){
 $data = null; $options=null;
 if($handle_myServer){
 $someFlagForGA = 'RequestFromSpainServer';
 }
 if($action == 'event'){
 $data = array('v' => '1'
 , 'tid' => 'UA-HIDDEN-1', 
 ,'cid' => $request_ip
 ,'ni' => '1'
 , 't' => 'event'
 , 'ec' => $someFlagForGA, 
 ,'ea' => 'JUMP', 'el' => ''
 );
 } elseif($action == 'pageview'){
 $data = array('v' => '1', 'tid' => 'UA-HIDDEN-1'
 , 't' => 'pageview'
 , 'dh'=> 'yourGAenabledDomainHere.com' 
 , 'dp'=> 'ViewedPage.html'
 , 'dt'=> 'homepage'.' SERVER VISITED '.$someFlagForGA
 );
 } else {
 // Do whatever else
 }
 // Would be better to do below with a single function 
 $options = array(
 'http' => array(
 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
 'method' => 'POST',
 'content' => http_build_query($data),
 ) ,$data);
 $context = stream_context_create($options);
 $result = file_get_contents($url, false, $context) or die('Error!!');
 }
?>
answered Dec 9, 2013 at 6:03

2 Comments

If I link to other site, and add that onclick method, will it work? for example: <a href=google.com onclick="ga(blah blah">click me</a>. I thought that when I click on the link the browser would take me to google.com and since the page is unloaded it wouldn't take any care of the function. Am I wrong?
@ice24 - No, you can definitely grab that event before an external page is loaded (using preventDefault). I will update answer with js code for this if the server side code I showed is of no use to you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.