전체 프로덕션 환경에서 이것을 테스트하지는 않았지만 jQuery의이 작은 스 니펫은 사용자 정의 가능한 클래스 이름이있는 링크를 구문 분석하여 동일한 Google Analytics 이벤트 추적 코드로 만드는 좋은 방법을 제공하는 것으로 보입니다.Google 애널리틱스 이벤트 추적 통합 누구든지 이보다 더 좋은 해결책을 찾았습니까?
이 방법이 효과가 있습니까? 내가 여기서 더 잘할 수있는 건?
jQuery(function(){
$("a.track").each(function(){
var obj = $(this); //provide scope
obj.data("href", obj.attr("href"));
obj.data("target", obj.attr("target"));
obj.attr({"target": ""});
obj.attr({"href": "#" + obj.data("href") });
obj.click(function(){
var find = {c: "cat_", a:"action_", l:"label_", d:"data_"}; //define your class prefixes
var p = {};
$.each(obj.attr('class').split(/\s+/), function(i,v){
$.each(find, function(a, b){
if(v.indexOf(b) == 0){ p[a] = v.replace(b, "").replace("__", " "); } //double underscore converted to space
});
});
track(p); Log("tracking...");
if(obj.data('target') == "_blank"){
var newWindow = window.open(obj.data("href"), '_blank');
newWindow.focus();
}
else{ window.location = obj.data("href");
}
});
});
function track(i){
if(i==undefined || !i["c"] || !i["a"]){Log("Both the 'category' and 'action' are required to track events!"); return;}
var d = {c: "Category", a:"Action", l:null, d:null}; //set default values
var p = $.extend(d, i); //merge function data with defaults
_gaq.push(['_trackEvent', p.c, p.a, p.l]);
}
function Log(x){try{console.log(x);}catch(e){}}
});
감사를 공유! –