計測タグをカスタマイズする

条件付きでtracker.initを発火する

計測UU調整のためなど、必ずしも全ての来訪者を計測したくない場合は、管理画面から発行される計測タグをカスタマイズし、tracker.initを条件付きで発火することができます。

注意事項

  • カスタマイズは必ずお客様の責任において行ってください。
  • 下記はサンプルコードとなり、そのままお客様環境に設置しても動作しません。必ず管理画面から発行される計測タグをカスタマイズしてください。

サンプル

セッション累計○PV目から計測する

例:セッション累計3PV目から計測する

<script type="text/javascript">(function(){var t,e,n,r,a;for(t=function(){var t;return t=[],function(){var e,n,r,a;for(n=["init","start","stop","user","track","action","event","goal","chat","buy","page","view","admin","group","alias","ready","link","form","click","submit","cmd","emit","on","send","css","js","style","option","get","set","collection"],e=function(e){return function(){return t.push([e].concat(Array.prototype.slice.call(arguments,0)))}},r=0,a=[];r<n.length;)t[n[r]]=e(n[r]),a.push(r++);return a}(),t.init=function(e,n){var r,a;return t.api_key=e,t.options=n||{},a=document.createElement("script"),a.type="text/javascript",a.async=!0,a.charset="utf-8",a.src=t.options.tracker_url||"https://static.karte.io/libs/tracker.js",r=document.getElementsByTagName("script")[0],r.parentNode.insertBefore(a,r)},t},r=window.karte_tracker_names||["tracker"],e=0,n=r.length;n>e;e++)a=r[e],window[a]||(window[a]=t());                             
try{
    var FLG_NAME = "krt.__pv";
    var PV_THRESHOLD = 3;
    
    var getPV = function(){
      var v = sessionStorage.getItem(FLG_NAME);
      if(v === null){ return 1; }
      var pv = Number(v);
      if(isNaN(pv)){ return 1; }
      return pv+1;
    }
    if(!sessionStorage || !sessionStorage.getItem){
      return;
    }
    var pv = getPV();
    if(pv >= PV_THRESHOLD){
      tracker.init('#{API_KEY}');
    }else{
      sessionStorage.setItem(FLG_NAME, pv);
    }
  }catch(e){}
}).call(this);</script>

ブラウザ累計○PV目から計測する

例:1週間以内に3PV以上あったユーザーのみ計測する。7日以上間が開いた場合はカウントをリセットし、再び3PVがカウントされるまで計測をしない。

<script type="text/javascript">(function(){var t,e,n,r,a;for(t=function(){var t;return t=[],function(){var e,n,r,a;for(n=["init","start","stop","user","track","action","event","goal","chat","buy","page","view","admin","group","alias","ready","link","form","click","submit","cmd","emit","on","send","css","js","style","option","get","set","collection"],e=function(e){return function(){return t.push([e].concat(Array.prototype.slice.call(arguments,0)))}},r=0,a=[];r<n.length;)t[n[r]]=e(n[r]),a.push(r++);return a}(),t.init=function(e,n){var r,a;return t.api_key=e,t.options=n||{},a=document.createElement("script"),a.type="text/javascript",a.async=!0,a.charset="utf-8",a.src=t.options.tracker_url||"https://static.karte.io/libs/tracker.js",r=document.getElementsByTagName("script")[0],r.parentNode.insertBefore(a,r)},t},r=window.karte_tracker_names||["tracker"],e=0,n=r.length;n>e;e++)a=r[e],window[a]||(window[a]=t());                             
    var FLG_NAME = "krt.__pv_date";
    var DELIMITER = '::';
    var PV_THRESHOLD = 3;
    var DAYS = 7;
    
    var getStorageValue = function(){
    var defaultValue = {pv: 1, firstVisitDate: new Date().getTime()};
    
    var v = localStorage.getItem(FLG_NAME);
    if(v === null){ return defaultValue; }
    var splited = v.split(DELIMITER);
    if(!splited[0] || !splited[1] || isNaN(Number(splited[0])) || isNaN(Number(splited[1]))){
      return defaultValue;
    }
    var pv = Number(splited[0]);
    var firstVisitDate = Number(splited[1]);
    
    if( new Date().getTime() > new Date(firstVisitDate).setDate(new Date(firstVisitDate).getDate() + DAYS)){
      return defaultValue;
    }
    return {pv: pv+1, firstVisitDate: firstVisitDate};
    }
    if(!localStorage || !localStorage.getItem){
      return;
    }
    var value = getStorageValue();
    if(value.pv >= PV_THRESHOLD){
      tracker.init('#{API_KEY}');
    }else{
      localStorage.setItem(FLG_NAME, value.pv+DELIMITER+value.firstVisitDate);
    }
}).call(this);</script>

計測するユーザーを確率で制御する

例:50%のユーザーを計測する

<script type="text/javascript">(function(){var t,e,n,r,a;for(t=function(){var t;return t=[],function(){var e,n,r,a;for(n=["init","start","stop","user","track","action","event","goal","chat","buy","page","view","admin","group","alias","ready","link","form","click","submit","cmd","emit","on","send","css","js","style","option","get","set","collection"],e=function(e){return function(){return t.push([e].concat(Array.prototype.slice.call(arguments,0)))}},r=0,a=[];r<n.length;)t[n[r]]=e(n[r]),a.push(r++);return a}(),t.init=function(e,n){var r,a;return t.api_key=e,t.options=n||{},a=document.createElement("script"),a.type="text/javascript",a.async=!0,a.charset="utf-8",a.src=t.options.tracker_url||"https://static.karte.io/libs/tracker.js",r=document.getElementsByTagName("script")[0],r.parentNode.insertBefore(a,r)},t},r=window.karte_tracker_names||["tracker"],e=0,n=r.length;n>e;e++)a=r[e],window[a]||(window[a]=t());                             
try{
    var FLG_NAME = "krt.__rnd";
    var PARCENTAGE = 50;
    if(!localStorage || !localStorage.getItem){
      return;
    }

    var rnd = localStorage.getItem(FLG_NAME);
    if(rnd==null){
      rnd = Math.ceil(Math.random()*100);
      localStorage.setItem(FLG_NAME, rnd);
    }
    
    if(Number(rnd) <= PARCENTAGE){
      tracker.init('#{API_KEY}');
    }
  }catch(e){}
}).call(this);</script>

複数サブドメインを考慮した計測するユーザーを考慮した計測タグ

例:50%のユーザーを計測する

<script type="text/javascript">(function(){var t,e,n,r,a;for(t=function(){var t;return t=[],function(){var e,n,r,a;for(n=["init","start","stop","user","track","action","event","goal","chat","buy","page","view","admin","group","alias","ready","link","form","click","submit","cmd","emit","on","send","css","js","style","option","get","set","collection"],e=function(e){return function(){return t.push([e].concat(Array.prototype.slice.call(arguments,0)))}},r=0,a=[];r<n.length;)t[n[r]]=e(n[r]),a.push(r++);return a}(),t.init=function(e,n){var r,a;return t.api_key=e,t.options=n||{},a=document.createElement("script"),a.type="text/javascript",a.async=!0,a.charset="utf-8",a.src=t.options.tracker_url||"https://static.karte.io/libs/tracker.js",r=document.getElementsByTagName("script")[0],r.parentNode.insertBefore(a,r)},t},r=window.karte_tracker_names||["tracker"],e=0,n=r.length;n>e;e++)a=r[e],window[a]||(window[a]=t());
  try {
    var FLG_NAME = "krt.__rnd";
    var PARCENTAGE = 50;
    // 貴社ドメイン名を指定
    var DOMAIN = "XXXXXXX";

    var rnd = + (function() {
      if (document.cookie.length === 0) return "";
      var c_start = document.cookie.indexOf(" " + FLG_NAME + "=");
      if (c_start === -1) return "";
      c_start = c_start + FLG_NAME.length + 2;
      var c_end = document.cookie.indexOf(';', c_start);
      if (c_end === -1) c_end = document.cookie.length;
      return document.cookie.substring(c_start, c_end);
    }());
    if (!rnd) rnd = Math.ceil( Math.random() * 100 );

    var exdate = new Date();
    exdate.setFullYear(exdate.getFullYear() + 2);
    document.cookie = FLG_NAME + "=" + rnd + "; domain=" + DOMAIN + "; expires=" + exdate.toUTCString() + "; path=/; samesite=strict";

    if (rnd <= PARCENTAGE){
      tracker.init('#{API_KEY}');
    }
  } catch(e) {}
}).call(this);</script>