dotMobimobiThinkingmobiForgemobiReadyDeviceAtlasfind.mobiInstant Mobilizer

Posted by mumme 1 year 43 weeks ago

pic
 mumme
mobiForge Newbie
Posts: 1
Joined: 1 year ago
[offline]

I am trying to get some statistics with my mobile site, so I extend the Device Atlas API. What do you think about this PHP code?

$properties = da_get_properties($tree, $_SERVER['HTTP_USER_AGENT']);
 
	$var_utmac='UA-000000-00'; //enter the new urchin code
	$var_utmhn='devi.mobi'; //enter your domain
	$var_utmn=rand(1000000000,9999999999); //random request number
	$var_cookie=rand(10000000,99999999); //random cookie number
	$var_random=rand(1000000000,2147483647); //number under 2147483647
	$var_today=time(); //today
	$var_referer=$_SERVER['HTTP_REFERER']; //referer url
 
	$var_uservar='&ua='.urlencode($_SERVER['HTTP_USER_AGENT']); //enter your own user defined variable
	$var_uservar.='&vendor='.urlencode($properties[vendor]);
	$var_uservar.='&device='.urlencode($properties[_matched]);
	$var_uservar.='&model='.urlencode($properties[model]);
 
	$var_utmp=$_SERVER['PHP_SELF'];
 
	$sr = urlencode($properties[displayWidth].'x'.$properties[displayHeight]);  // screen size
	$sc = urlencode($properties[displayColorDepth].'-bit');  // color depth
	$ul = '?';  // user language, I am not setting
	$je = '0';  // support for javascrip, I am not setting
	$fl = urlencode($properties[vendor].'-'.$properties[_matched].'-'.$properties[model]);  // flash version, I am sending device information instead.  Don't know if it work yet
	$dt = $var_utmp;  // page title, I am sending page URL
 
	$urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=1&utmn='.$var_utmn.'&utmsr='.$sr.'&utmsc='.$sc.'&utmul='.$ul.'&utmje='.$je.'&utmfl='.$fl.'&utmdt='.$dt.'&utmhn='.$var_utmhn.'&utmr='.$var_referer.'&utmp='.$var_utmp.'&utmac='.$var_utmac.'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.$var_today.'.'.$var_today.'.'.$var_today.'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.$var_cookie.'.'.$var_uservar.'%3B';
 
	$handle = fopen ($urchinUrl, "r");
	$test = fgets($handle);
 
	fclose($handle);

Posted by skolesnyk 1 year ago

pic
 skolesnyk
mobiForge Newbie
Posts: 4
Joined: 1 year ago
[offline]

Did you take the code from this http://www.vdgraaf.info/google-analytics-without-javascript.html and expanded it?

Anyways, I'll try it on my mobile site and will tell you the results in day.

Posted by skolesnyk 1 year ago

pic
 skolesnyk
mobiForge Newbie
Posts: 4
Joined: 1 year ago
[offline]

I changed it a bit for tera-wurfl use. But looks like it's working...

Posted by atrasatti 1 year ago

pic
 atrasatti
dotMobi logo
Mobile Genius
Posts: 320
Joined: 3 years ago
[offline]

Very interesting piece of code.

I've installed this on a test site. It's still not perfect for me, I need to debug a bit, but seems to be sending data to Google. I'll post more feedback here as I improve the script.

Very good tip.

Andrea Trasatti
DeviceAtlas, mobile device intelligence

Posted by skolesnyk 1 year ago

pic
 skolesnyk
mobiForge Newbie
Posts: 4
Joined: 1 year ago
[offline]

I put it on mine and setup device filltering like this

Field A -> Extract A :User Defined: .*brand=(.*)&model=(.*)

Output to -> Constructor:Visitor Browser Program: $A1 / $A2

I still have to figure out how to transmit user IP address to google for geo location decoding.

Andrea, did you manage to do it?

Posted by Fareed (not verified) 1 year ago

pic
 
Mobile Master
Posts: 82
Joined:
[offline]

This comment has been moved here.

Posted by dbl 1 year ago

pic
 dbl
mobiForge Newbie
Posts: 1
Joined: 1 year ago
[offline]

If you replace the last 3 lines in your code by these

if ((isset($_SERVER['HTTP_USER_AGENT'])))
$useragent = $_SERVER['HTTP_USER_AGENT'];
else
$useragent = 'UNKNOWN_HTTP_USER_AGENT';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urchinUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_exec($ch);
curl_close($ch);

From now you can send the USER_AGENT from your manually-custom request to analytics.

Cheers.

Posted by kbelange (not verified) 1 year ago

pic
 
Mobile Master
Posts: 82
Joined:
[offline]

Does the above method still work?

I've set up a Unit Test based on Peter v.d. Graaf's example that's not generating any Google Analytics data, although the HTTP request/response appears to work:

Get Google Analytics...

GA get request:

http://www.google-analytics.com/__utm.gif?utmwv=1
&utmn=1259442976
&utmsr=-
&utmsc=-
&utmul=-
&utmje=0
&utmfl=-
&utmdt=-
&utmhn=tha.artlogic.com
&utmr=
&utmp=/scripts/TestAGoogleAnalyticsOriginalCode_html.php
&utmac=UA-4635840-1
&utmcc=__utma%3D35105132.1106525547.1213378550.1213378550.1213378550.2%3B%2B__utmb%3D35105132%3B%2B__utmc%3D35105132%3B%2B__utmz%3D35105132.1213378550.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D35105132.-%3B

GA get response: GIF89a�����,D;

TestCase AGoogleAnalyticsOriginalCodeTestCase->testGoogleAnalytics() passed

Posted by kbelange (not verified) 1 year ago

pic
 
Mobile Master
Posts: 82
Joined:
[offline]

are there any tricks to setting the cookie number so that google correctly recognizes user sessions?

GA is still not recognizing user sessions: i.e, each hit is seen as an Absolute Unique Visitor, so none of the Visitor Trending or site crawling stats make sense

here are the cookie values i'm sending, with a uniquely generated session id of: "73305051" in this case (my session management code also handles cookieless browsers):

&utmcc=__utma%3D73305051.1572483978.1215112280.1215112280.1215112280.2%3B%2B
__utmb%3D73305051%3B%2B__utmc%3D73305051%3B%2B__utmz%3D73305051.1215112280.2.2.
utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D73305051.unit+test%3B

my mostly working code is a version of Ian Lewis' customization of Peter v.d. Graaf's example

in case it helps others, the tricks that got the USER_AGENT working for me were:

1) using CURL as above (thanks to 'dbl'):

// using curl()
$handle = curl_init($this->fUrchinUrl);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, $httpOptions);
curl_setopt($handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$this->fContent = curl_exec($handle);
curl_close($handle);

2) being careful to set the Host in the HTTP headers like so:

$header = 'Host: www.google-analytics.com\r\n';

if (!empty($_SERVER['HTTP_USER_AGENT']))
{
$header .= 'User-Agent: '.$_SERVER['HTTP_USER_AGENT'].'\r\n';
}
//Set the language to that of the client so analytics can track it.
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
$header .= 'Accept-language: '.$_SERVER['HTTP_ACCEPT_LANGUAGE'].'\r\n';
}

$httpOptions = array(
'http'=>array(
'method'=>'GET',
'header'=>$header,
'request_fulluri'=>'true'
)
);

Posted by dpowers 1 year ago

pic
 dpowers
mobiForge Newbie
Posts: 2
Joined: 1 year ago
[offline]

How would one set the page that the user leaves from?
How would you set how long the user spent on a page? Maybe set a session when the user arrives on the page and then check that session when they arrive on the next page and subtract it from the current time, BUT then you would not update the GA for the previous page!

I would think it might be an idea to create GALogging.php script that would run on a cron and loop through a log file. This way for each record in the log file it can look ahead to the next log for the same session if there is one then is can check the time and see how long the user spent on the current page/log and if there is not another log for that session then it knows that the current log/session was the page the user exited on.
It would loop through each long and use curl to send the info to GA.

Any thoughts?