金. 3月 29th, 2024

例えば、

http://www.domain.com/

というURLのサイトと、同じドメインのサブディレクトリにある別サイト

http://www.domain.com/directory/

があった場合、かつ両サイトともにそれぞれ別のプロパティ(WebプロパティID)でGoogleアナリティクスを利用していた場合、ちょっと注意が必要です。

ご存知の通り、Cookieはドメインとパスで管理されています。そして、Googleアナリティクスはユーザの識別や参照元の情報にCookieを使用しています。

上記のURLの場合、Googleアナリティクスのデフォルトでは下記のようにドメインとパスを指定します。

ドメイン: .domain.com
パス: /

そして、このパスはこのCookieを適用するディレクトリの最上位のパスを示しています。つまり解析対象ページのパスが「/」配下でも、「/directory/」配下でも、同じCookieが適用されるのです。

さらに厄介なことに、このCookie情報にはWebプロパティIDの情報が含まれていません。つまりドメインとパスの指定範囲内のURLであれば、例えばGoogleアナリティクスのプロパティが別であっても、同じCookieが利用されるのです。

これがどういう問題を起こすかというと、例えば上記の例のようなサイト構成になっていた場合で、下記のような遷移をユーザが行った場合。

検索エンジンで「meaningfree」を検索 → http://www.domain.com/にランディング → http://www.domain.com/directory/に移動

この場合、http://www.domain.com/directory/のサイトの参照元には、「http://www.domain.com/」と記録されるべきですが、実際には「meaningfreeというキーワードで検索」という参照元が記録されてしまいます。

もう1つ例を言うと、www.domain.comのドメイン配下のサイトに一度も訪れたことがない、つまり新規ユーザが、

  • 検索エンジンで「meaningfree」を検索 → http://www.domain.com/にランディング → サイト離脱
  • 次の日に検索エンジンで「meaningfree 最高」を検索 → http://www.domain.com/directory/にランディング

という2つの遷移をした場合、http://www.domain.com/directory/のサイトへのアクセスも新規ユーザとして記録されるべきなのですが、これはリピートユーザになってしまいます。

 

ということで、こういう場合は_setCookiePath()というGoogleさんが用意してくれた関数を使って、Cookieのパスをサブディレクトリに制限しましょう。

[html title=”_setCookiePath()ソース例” highlight=”4″]

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-00000000-1’]);
_gaq.push([‘_setCookiePath’, ‘/directory/’]);
_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>

[/html]

また、

1つのサイトが/directory/と/directory2/にまたがる場合には、_cookiePathCopy()でCookieをもう1つのディレクトリにコピーしましょう。

[html title=”/directory/配下のページのソース例” highlight=”4,6″]
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-00000000-1’]);
_gaq.push([‘_setCookiePath’, ‘/directory/’]);
_gaq.push([‘_trackPageview’]);
_gaq.push([‘_cookiePathCopy’, ‘/directory2/’]);

(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>

[/html]
[html title=”/directory2/配下のページのソース例” highlight=”4,6″]
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-00000000-1’]);
_gaq.push([‘_setCookiePath’, ‘/directory2/’]);
_gaq.push([‘_trackPageview’]);
_gaq.push([‘_cookiePathCopy’, ‘/directory/’]);

(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>

[/html]

です。

特に2つのサイトの制作会社が違う場合、上記のようなCookie共用問題が発生してしまいがちです。
本当に気をつけましょう。本当に…。

5 thoughts on “【GA】同じドメイン内に別サイトがある場合には、_setCookiePath()をしよう”
    1. rai様

      お返事遅くなってすいません。
      色々と検証していたところ、コメント欄に収まりきらなかったためブログ記事にしました。

      こちらをご覧ください。
      http://meaningfree.net/?p=283

      よろしくお願いいたします。

  1. 1つのサイトが/directory/と/directory2/に加えて、
    /directory3/。。。と増えていく場合は、
    _cookiePathCopy()で追加していけばよいのでしょうか?

    1. tk様

      未検証なので申し訳ないのですが、
      _cookiePathCopy()を追加していくということで大丈夫かと思います。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です