javascript 在Classic或Universal Analytics中跟踪Google Analytics事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 在Classic或Universal Analytics中跟踪Google Analytics事件相关的知识,希望对你有一定的参考价值。

//This function tracks a google analytics event regaurdless if using UA or classic trackin code
/**
 * Tracks a GA event using either Classic or Universal Analytics.
 * @param  {[String or Object]} categoryorobject [Either Event Category, or an object containing all parameters]
 * @param  {[String]} action           [The Event Action]
 * @param  {[String]} label            [The Event Lable]
 * @param  {[String]} value            [The Event Value]
 */
function trackEvent(categoryorobject, action, label, value) {
  if(typeof(ga) == "function") {
    //check argument length to determine parameters
    switch(arguments.length) {
      case 1:
        //If there is one parameter, it can only be an object to define parameters
        if(typeof categoryorobject == "object" && categoryorobject.eventCategory && categoryorobject.eventAction) {
          //Send it off like it is!
          categoryorobject.hitType = 'event';
          return ga('send', categoryorobject);
          break;
        }
      case 2:
        return ga('send', 'event', categoryorobject, action);
        break;
      case 3:
        return ga('send', 'event', categoryorobject, action, label);
        break;
      case 4:
        return ga('send', 'event', categoryorobject, action, label, value);
        break;
      default:
        //Oops!
        return false;
    }
  } else if(typeof(_gaq) == "object") {
    switch(arguments.length) {
      case 1:
        //If there is one parameter, it can only be an object to define parameters
        if(typeof categoryorobject == "object") {
          //The onyl reason you may need to pass an object, is to define the hitcallback, so let's set it
          if(categoryorobject.hitCallback && typeof categoryorobject.hitCallback == "function") {
            _gaq.push(['_set', 'hitCallback', function(){
              categoryorobject.hitCallback.apply();
            }]);
          }
          //Check remaining object properties for optional parameters
          //Will make a recursive call with properties passed as normal parameters
          if(categoryorobject.eventValue && categoryorobject.eventLabel && categoryorobject.eventAction && categoryorobject.eventCategory) {
            trackEvent(categoryorobject.eventCategory,categoryorobject.eventAction,categoryorobject.eventLabel,categoryorobject.eventValue);
          } else if(categoryorobject.eventLabel && categoryorobject.eventAction && categoryorobject.eventCategory) {
            trackEvent(categoryorobject.eventCategory,categoryorobject.eventAction,categoryorobject.eventLabel);
          } else if(categoryorobject.eventAction && categoryorobject.eventCategory) {
            trackEvent(categoryorobject.eventCategory,categoryorobject.eventAction);
          }
        }
      case 2:
        return _gaq.push(['_trackEvent', categoryorobject, action]);
        break;
      case 3:
        return _gaq.push(['_trackEvent', categoryorobject, action, label]);
        break;
      case 4:
        return _gaq.push(['_trackEvent', categoryorobject, action, label, value]);
        break;
      default:
        //Oops!
        return false;
    }    
  }
}





/**
 * Here's an example for tracking an event using hitCallback
 */
trackEvent(	{'eventCategory':'quote-form',
			'eventAction':'submitted',
			'eventLabel':'contact-me',
			'eventValue':0,
			'hitCallback': function() {
            		thisForm.submit(); //Called after event is logged
                }	
			});

//Without hitcallback we can use the standard parameters
trackEvent('quote-form','submitted','contact-me',0);

以上是关于javascript 在Classic或Universal Analytics中跟踪Google Analytics事件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 asp classic 中使用 ionic zip 或 dot net zip

通过 Classic 或 Rest API 的 Paypal 付款

php 使Elementor成为默认编辑器,而不是WordPress编辑器(Gutenberg或Classic)

php 使Elementor成为默认编辑器,而不是WordPress编辑器(Gutenberg或Classic)

为 EC2-Classic 中的负载均衡器添加或删除可用区

Mifare Classic Tool读水卡扇区显示没密钥发现(或死扇区)怎么回事