Google Analyticsでアンドロイドの機種名を取得

Google AnalyticsのPCのトラッキングコードを入れておけば、Androidもトラッキングできるのですが、

OS・・Android
OSのバージョン・・(携帯では機種名やデバイスIDが表示される)Andorid OSのバージョン

が表示されるのですが、機種名(IS01とかIS03とか)を知る術がなく・・・

そこで、GA(Google Analytics)を使って機種名を取得できるようにしてみました。

元々のトラッキングコード

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

変更後のトラッキングコード

<script type="text/javascript">

//ここを入れます
var myArray = navigator.userAgent.split(' ');
if (myArray[6] == "Docomo"){
myArray[6] = myArray[6]+myArray[7]; //HT-03A
}
//ここまで

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_setCustomVar',1,'Android',myArray[6],2]); //カスタム変数に入れます
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

UserAgantを半角スペースで配列に入れて、その6番目をカスタム変数で入れています。

ただ、HT-03AのAndroidバージョン1.6だけは、

Mozilla/5.0 (Linux; U; Android 1.6; ja-jp; Docomo HT-03A Build/DRD08) AppleWebKit?/528.5+(KHTML, like Gecko) Version/3.1.2 Mobile Safari/ 525.20.1

Docomo HT-03Aとなっていて、スペース区切りじゃDocomoだけしか入らないので、ちょっと調整しています。
Android1.5のときは、HT-03A だけだったのになぁ。。

参考サイト

上記の設定をしたのにもかかわらず、なぜか、ユーザー定義に数値が出てくるのがなぞだけど・・・

Google Analyticsでアンドロイドの機種名を取得

数値取得できるようになりました。

Androidの機種名だけじゃなくて、iPhoneや、iPod touchのOSバージョン情報も同じ設定で取得できました。便利。

ちなみに、今、AndroidやiPhone iPod tocuhで晄夏ノートみると、一番下にAndroidなら機種名、iPhoneとかならバージョンが表示できるようになっております。