Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>An event in and of itself is not a pageview.</strong> See: <a href="https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide?csw=1" rel="nofollow">Event Tracking</a></p> <p>Is there a specific reason why you need to track events server side and pageviews from the normal ga.js client-side code?</p> <p>You can easily track events from the client side, if you were unaware of that: <code>&lt;a href="#" onClick="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);"&gt;Click Link to Track Event&lt;/a&gt;</code></p> <p>Assuming that you needed to keep events AND pageviews on the server side:</p> <pre><code>&lt;?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'; </code></pre> <p>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)</p> <pre><code> $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'); } </code></pre> <p>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!</p> <pre><code> foreach($actions as $action){ $data = null; $options=null; if($handle_myServer){ $someFlagForGA = 'RequestFromSpainServer'; } if($action == 'event'){ $data = array('v' =&gt; '1' , 'tid' =&gt; 'UA-HIDDEN-1', ,'cid' =&gt; $request_ip ,'ni' =&gt; '1' , 't' =&gt; 'event' , 'ec' =&gt; $someFlagForGA, ,'ea' =&gt; 'JUMP', 'el' =&gt; '' ); } elseif($action == 'pageview'){ $data = array('v' =&gt; '1', 'tid' =&gt; 'UA-HIDDEN-1' , 't' =&gt; 'pageview' , 'dh'=&gt; 'yourGAenabledDomainHere.com' , 'dp'=&gt; 'ViewedPage.html' , 'dt'=&gt; 'homepage'.' SERVER VISITED '.$someFlagForGA ); } else { // Do whatever else } // Would be better to do below with a single function $options = array( 'http' =&gt; array( 'header' =&gt; "Content-type: application/x-www-form-urlencoded\r\n", 'method' =&gt; 'POST', 'content' =&gt; http_build_query($data), ) ,$data); $context = stream_context_create($options); $result = file_get_contents($url, false, $context) or die('Error!!'); } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload