2009年11月アーカイブ
最近Google AnalyticsのMobile版がひっそりと始まりましたね。
ホントにひっそりで、場所がわかり辛いのですが、
https://www.google.com/analytics/settings/check_status_profile_handler
ここの、
導入方法 Perl編
ん?
あれ、docomo以外みてくれてない・・
というわけで修正しました。独自でやってもできるけどキャリア判別とかが煩雑だったので、HTTP::MobileAgent使っています。0.27以降がdocomoID対応なんで、それ以降を使います。
当て方はga.plと上のga.pl.patchを同じディレクトリに置いて
ホントにひっそりで、場所がわかり辛いのですが、
https://www.google.com/analytics/settings/check_status_profile_handler
ここの、
- Instructions for adding tracking
- Advancedタブ
- 1 What are you tracking?
- A site built for a mobile phone
導入方法 Perl編
- 「2 Paste code on your mobile site」のコードをviewから呼び出せるところに準備
- htmlあたりに<img src="[% google_analytics_get_image_url() %]">書く
- 「3 Copy this file to your root directory」にあるga.plをcgi起動出来るように設定するか、同様の内容を作る
なんでかと思い調べたところ・・・
さっそく導入してみたんですが、どうもsessionの同定が出来てないっぽいです。
別集計で出したUUが2,000ぐらいなのに、Analyticsだと10,000になっていたりします。
# Generate a visitor id for this hit.
# If there is a visitor id in the cookie, use that, otherwise
# use the guid if we have one, otherwise use a random number.
sub get_visitor_id {
my ($guid, $account, $user_agent, $cookie) = @_;
# If there is a value in the cookie, don't change it.
if ($cookie ne "") {
return $cookie;
}
my $message = "";
if ($guid ne "") {
# Create the visitor id using the guid.
$message = $guid . $account;
} else {
# otherwise this is a new user, create a new random id.
$message = $user_agent . get_random_number();
}
my $md5_string = md5_hex($message);
return "0x" . substr($md5_string, 0, 16);
}
ん?
# If there is a visitor id in the cookie, use that, otherwise
# use the guid if we have one, otherwise use a random number.
あれ、docomo以外みてくれてない・・
というわけで修正しました。独自でやってもできるけどキャリア判別とかが煩雑だったので、HTTP::MobileAgent使っています。0.27以降がdocomoID対応なんで、それ以降を使います。
--- ga.pl 2009-11-12 21:27:23.000000000 +0900
+++ ga.pl.new 2009-11-12 21:22:04.000000000 +0900
@@ -6,6 +6,7 @@
use Digest::MD5 qw(md5_hex);
use LWP::UserAgent;
use URI::Escape;
+use HTTP::MobileAgent;
use strict;
# Tracker version.
@@ -59,9 +60,10 @@
}
my $message = "";
- if ($guid ne "") {
- # Create the visitor id using the guid.
- $message = $guid . $account;
+ my $user_id = _get_user_id();
+ if ($user_id ne "") {
+ # Create the visitor id using the uesr_id.
+ $message = $user_id . $account;
} else {
# otherwise this is a new user, create a new random id.
$message = $user_agent . get_random_number();
@@ -192,4 +194,14 @@
write_gif_data($new_cookie, $utm_url);
}
+sub _get_user_id {
+ my $agent = HTTP::MobileAgent->new;
+ return $agent->user_id
+ if $agent->is_docomo;
+ return (split('_', $ENV{HTTP_X_UP_SUBNO}, 2))[0]
+ if $agent->is_ezweb;
+ return substr($ENV{HTTP_X_JPHONE_UID}, 1, length($ENV{HTTP_X_JPHONE_UID}))
+ if $agent->is_vodafone;
+}
+
track_page_view();
当て方はga.plと上のga.pl.patchを同じディレクトリに置いて
$ patch -p0 < ga.pl.patch
このパッチでは、端末ID送信しない設定の端末とか、端末ID送信しないwillcomとかをスルーしちゃいます。厳密に計測するには、アプリケーション側で端末IDみて、なければユニークなID(session id)とかを使うべきですが、これだけでもそこそこの制度がでるので、さっくりやりたい人はこっちで良いと思います。















