使用 JQuery 评级星级并将数据提交给分析

发布于 2024-12-16 20:16:29 字数 2289 浏览 4 评论 0原文

我在实现 JQuery 星级评分插件时遇到一些问题。有问题的插件可以在 http://orkans-tmp.22web.net/star_ rating/

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

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

<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"> 

号,这就是为什么我在它上面添加了一个点击功能,以尝试提交给定的分数。

如果有人以前有过使用该插件的经验,那就太好了。

谢谢,

西蒙

I am having some trouble implementing a JQuery star rating plugin. The plugin in question can be found at http://orkans-tmp.22web.net/star_rating/

The trouble I'm having is passing the score off to Google analytics. I have managed to implement the plugin but for some reason the code will not fire the analytics custom event. The correct current value is alerted when the stars are clicked, which is the value I want to pass to the Google analytics event tracking.

Any help would be much appreciated, my code is as follows:

<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/>

The plugin creates a

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

for each star which is why I put a click function on it, to try and submit the score given.

If anyone has any previous experience with the plugin that would be great.

Thanks,

Simon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-12-23 20:16:29

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

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

感谢您的输入 Rory。

I found the answer to my problem. It appears the currValue was being parsed as a String, not an integer which is required by analytics to fire the script. I solved it by converting the value into an integer using the following code:

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

Thanks for your input Rory.

此刻的回忆 2024-12-23 20:16:29

如果带有 .ui-stars-star 类的元素是动态创建的,请尝试使用 live() 方法来连接它们的事件:

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

或者,如果您使用的是 jQuery 1.7、可以使用on()

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

If the elements with the .ui-stars-star class are being created dynamically try the live() method to hook up their events:

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

Or, if you're using jQuery 1.7, you can use on()

$('.ui-stars-star').on("click", function() { ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文