Analytics
2K members online now
2K members online now
Learn how to set up goals and Ecommerce tracking, verify accuracy of attribution traffic sources, and discuss funnel visualization, Multi-Channel Funnels, the Goal Flow Report, and Enhanced Ecommerce
 
Guide Me
star_border
Reply

How to fire a "pageview" event without webpage

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi, I am trying to meter an app API usage using google analytics. In the server API code (php), I am trying to trigger an event like  ga('send', 'pageview');

but there is not webpage based, can't run client side javascript. Anyway we can get this implemented?

Such that when the event was triggered, google analytics will log that in the "customer dimension" part?

Thanks

Summer

1 Expert replyverified_user

Re: How to fire a "pageview" event without webpage

Top Contributor
# 2
Top Contributor
You need to use the Measurement Protocol, which is a developer tool:
https://developers.google.com/analytics/devguides/collection/protocol/v1/
Mike Sullivan, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: How to fire a "pageview" event without webpage

MWA
Follower ✭ ☆ ☆
# 3
MWA
Follower ✭ ☆ ☆
Sample PHP Code
<?php
$fields = array(
'payload_data' => '',
'v' => 1,
'tid' => $tid,
'cid' => $cid,
't' => "event",
'ec' => $ecategory,
'ea' => $eaction,
'el' => $elabel,
'ev' => $evalue);
$params=implode('&',$fields);
$push = curl_call("http://www.google-analytics.com/collect?payload_data", $params, $user_agent);

function curl_call($url, $param, $user_agent)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/var/www/webserv/tmp/cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/var/www/webserv/tmp/cookies.txt");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url . $param);

$output = curl_exec($ch);

$info = curl_getinfo($ch);
//$_SESSION["curl_info"] = $info;
curl_close($ch);
return $info;
}
?>

Reference
https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters