Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script>
if(typeof jQuery != 'undefined'){
jQuery(document).ready(function($){
/* ----------------------------------------------------------------- */
// スクロール量の計測
/* ----------------------------------------------------------------- */
$(function(){
/* ----------------------------------------------------------------- */
// 設定変更箇所
/* ----------------------------------------------------------------- */
// 計測のタイプ
// AreaPer: ページの高さに対するパーセンテージで指定
// AreaElms: タグ名・Class名・ID名で指定
var ga_slAreaType = "AreaElms"; // AreaPer or AreaElms
// AreaPerの場合のパーセント(10%なら0.1と指定。何個でも指定可能)
var ga_slAreaPer = new Array("0.1","0.2","0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0");
// AreaNumの場合の区切りとなる要素(jQueryのセレクタの記述ルールで指定。何個でも指定可能)
var ga_slAreaElms = [
'h3','.entry-footer'
];
// 固定のピクセル数で計測
// AreaPer・AreaElmsのどちらでも計測される。
// ファーストビューから少しでもスクロールしたかどうかを計測する。
var ga_slPointFixed = 150; // デフォルトは150px
/* ----------------------------------------------------------------- */
// 計測完了フラグ
var ga_slCmpFixed;
var ga_slCmp = new Array;
var ga_slCmpElms = new Array;
// 高さ・閾値関連
var ga_pg_h; // ページの高さ
var ga_win_h; // ウィンドウの高さ
var ga_slAreaSep; // エリアの閾値(配列)
var ga_slLabel; // イベントのラベル
var ga_slAreaSepElms; // エリアの閾値(配列)
var ga_slLabelElms; // イベントのラベル
function getHeight(){
// ページとウィンドウの高さを取得
ga_pg_h = $(document).height();
if(document.all){
ga_win_h = document.documentElement.clientHeight;
}else{
ga_win_h = window.innerHeight;
}
// 閾値の設定
ga_slAreaSep = [];
ga_slLabel = [];
ga_slAreaSepElms = [];
ga_slLabelElms = [];
var ga_slLabelnum = 1
var ga_slLabelnumElms = 1
var tagText = ""
for(var i = 0; i < ga_slAreaPer.length; i++){
ga_slAreaSep[i] = (ga_pg_h - ga_win_h) * ga_slAreaPer[i];
ga_slLabel[i] = ga_slAreaPer[i] * 100}
for(var i = 0; i < ga_slAreaElms.length; i++){
$(ga_slAreaElms[i]).each(function(){
ga_slAreaSepElms.push($(this).offset().top - ga_win_h);
tagText = $('h3').eq(ga_slLabelnumElms-1).text();
if(!tagText){
tagText = "End";
}else{
tagText = ga_slLabelnumElms + "_" + tagText;
}
ga_slLabelElms.push(tagText);
ga_slLabelnumElms = ga_slLabelnumElms + 1;
});
}
// 昇順にソート
ga_slAreaSepElms.sort(
function(a,b){
if(a < b){ return -1 };
if(a > b){ return 1 };
return 0;
}
);
}
getHeight();
$(window).resize(function(){
getHeight();
});
$(window).scroll(function(){
var ga_sl_y = $(this).scrollTop();
if((ga_sl_y >= ga_slPointFixed)&&(!ga_slCmpFixed)){
dataLayer.push({
'scrollPoint': 0,
'event': 'Scroll'
});
ga_slCmpFixed = true;
}
for (var i = 0; i < ga_slAreaSep.length; i++) {
if((ga_sl_y >= ga_slAreaSep[i])&&(!ga_slCmp[i])){
dataLayer.push({
'scrollPoint': ga_slLabel[i],
'event': 'Scroll'
});
ga_slCmp[i] = true;
}
}
for (var i = 0; i < ga_slAreaSepElms.length; i++) {
if((ga_sl_y >= ga_slAreaSepElms[i])&&(!ga_slCmpElms[i])){
dataLayer.push({
'scrollPointElms': ga_slLabelElms[i],
'event': 'ScrollElms'
});
ga_slCmpElms[i] = true;
}
}
});
});
/* ----------------------------------------------------------------- */
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.