2011-11-16 40 views
2

我在实现JQuery星级评分插件时遇到了一些麻烦。有问题的插件可以在http://orkans-tmp.22web.net/star_rating/使用JQuery评级星级并将数据提交到分析

我遇到的麻烦是将分数传递给谷歌分析。我设法实现了插件,但出于某种原因,代码不会触发分析自定义事件。单击星标时会提示正确的当前值,这是我想要传递给Google分析事件跟踪的值。

任何帮助将不胜感激,我的代码如下:

<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css"/> 
<script type="text/javascript" src="js/jquery.min.js></script> 
<script type="text/javascript" src="js/jquery-ui.custom.min.js"></script> 
<script type="text/javascript" src="js/jquery.ui.stars.js"></script> 
<link rel="stylesheet" type="text/css" href="css/jquery.ui.stars.css"/> 


<script type="text/javascript"> 
    var url = location.href; 

    $(function ratingStars(){ 
     $("#ratings").stars({ 
      captionEl: $("#stars-cap"), 
      oneVoteOnly: false, 
     }); 
    }); 

    $(document).ready(function(){ 
     $('.ui-stars-star').click(function() { 
      // Retrieve instance 
      var ui = $("#ratings").data("stars"); 
      var currValue = ui.options.value; 
      //alert the currValue to make sure it is in place. 
      alert(currValue); 
      //send data to track 
      _gaq.push(['_trackEvent', 'UATRatingTest', 'UATRatingTest2', url, currValue]); 
     }); 
    }); 
</script> 

<div class="RatingStars"> 
    <form id="ratings"> 
     <input type="radio" name="rate" value="1" title="Poor" id="rate1" /> 
     <input type="radio" name="rate" value="2" title="Fair" id="rate2" /> 
     <input type="radio" name="rate" value="3" title="Average" id="rate3" /> 
     <input type="radio" name="rate" value="4" title="Good" id="rate4" /> 
     <input type="radio" name="rate" value="5" title="Excellent" id="rate5" /> 
    </form> 
<span style="margin-right:5px; margin-left:10px;"> Rating: </span> <span id="stars-cap"></span> <br/> 

插件创建

<div class="ui-stars-star"> 

对于每个星星而言,这就是为什么我在其上放置了点击功能,尝试提交给定的分数。

如果任何人有任何以前的经验,将是伟大的插件。

感谢,

西蒙

回答

1

我找到了我的问题的答案。看来currValue被解析为一个字符串,而不是分析所需的整数来激发脚本。我通过使用以下代码将值转换为整数来解决此问题:

var myString = currValue; 
var myConvertedInteger; 
myConvertedInteger = parseInt(myString); 
//Check string has been converted 
alert(myConvertedInteger); 
_gaq.push(['_trackEvent', 'UATRatingTest', 'UATRatingTest2', url, myConvertedInteger]); 

感谢您的输入Rory。

0

如果与被动态创建的.ui-stars-star类元素尝试live()方法挂钩的事件:

$('.ui-stars-star').live("click", function() { ... 

或者,如果你使用jQuery 1.7,你可以使用on()

$('.ui-stars-star').on("click", function() { ... 
+0

你好,谢谢你的建议。不幸的是,这并没有使分析代码触发。我不认为解析分数有任何问题,因为警报显示正确的分数。它在分析中的一些东西并不火。不管怎么说,还是要谢谢你。 – Simon

+0

没问题。控制台/ Firebug中是否有任何与Google AJAX调用失败相关的内容? –

+0

不幸的是,脚本面板中没有错误,只是自定义事件不会向http发起http请求来记录自定义事件。 – Simon