本文介绍了无法将Google Analytics(分析)事件代码成功添加到由.attr创建的链接中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用由其他人创建的jQuery代码,并且我对jQuery(公正警告)非常陌生。

我需要将google事件标签添加到jQuery创建的链接中。这jQuery适用于模式弹出,其中包含各种大小的项目图标和静态按钮。当用户将鼠标悬停在图标上时,该按钮上的链接会发生变化,并且我需要将Google Analytics(分析)标记添加到每个图标的链接上,以便在按钮上进行更改。



以下是在特定图标悬停时创建按钮链接的代码:

<$ p $ ($(this).hasClass('selected')){
$(())$ h $('#c $ c'$''modal_sizes_4 .size_5' ''modal_sizes_4 .selected')。removeClass('selected');
$(this).addClass('selected');
$('#modal_sizes_4 .detail_left h3')。text('15 );
$('#modal_sizes_4 .detail_right img')。attr('');
$('#modal_sizes_4 .detail_left p')。 ({
src:'images / 5.4.5-product.jpg'
});
$('#modal_sizes_4 .detail_bottom a.shop_size')。attr({
href:'http://testing.com'
});
}
});


创建链接后,将其添加到链接的最佳方式是:

  onclick =_gaq.push(['_ trackEvent','CIGuide','点击商店尺寸','15 3/4项目']); > 

我曾尝试使用.click,.attr,.on和.bind,但没有运气。

如果我的问题没有完全合理,我很抱歉,我对这一切都非常陌生。
任何帮助,将不胜感激。我最终想要实现的是在google中的事件标记。谢谢。

解决方案

您可以使用以下代码添加调用:

  $(#my_element a).bind(mousedown,function(){
_gaq.push([_trackEvent,CIGuide点击Shop Size,15 3/4 Item]);
});

上面的代码适用于id为my_element的元素下的任何锚点:

 < h1> Hello World< / h1> 
< div id =my_element>
< h2>标题在这里< / h2>
< ul>
< li>
< a href =http://www.example.com>示例< / a>
< / li>
< li>
< a href =./ some-page.html>一些页面< / a>
< / li>
< li>
< a href =http://www.anotherexample.com>另一个示例< / a>
< / li>
< / ul>
< / div>
< a href =./ wont-fire>此连结不会触发事件< / a>

自定义选择符以符合您的目的,就是这样。


I am working with jQuery code created by someone else, and I am very new to jQuery (fair warning).

I need to add google event tags to the links the jQuery is creating. This jQuery is for a modal pop up that contains icons for various sizes of an item, and a static button. When the user hovers over the icon, the link on the button changes, and I need to add google analytics tagging to the link for each icon when it changes on the button.

Here is the code that creates the link for the button when a particular icon is hovered over:

$('#modal_sizes_4 .size_5').hover(function() {
    if (!$(this).hasClass('selected')) {
        $('#modal_sizes_4 .selected').removeClass('selected');
        $(this).addClass('selected');
        $('#modal_sizes_4 .detail_left h3').text('15 3/4" Item');
        $('#modal_sizes_4 .detail_left p').text('Good for many things.');
        $('#modal_sizes_4 .detail_right img').attr({
            src: 'images/5.4.5-product.jpg'
        });
        $('#modal_sizes_4 .detail_bottom a.shop_size').attr({
            href: 'http://testing.com'
        });
    }
});

​What is the best way to add this to the link once it's created:

onclick = "_gaq.push(['_trackEvent','CIGuide', 'Click Shop Size', '15 3/4 Item']);" >​

I have tried using .click, .attr, .on and .bind with no luck.

I apologize if my question doesn't make perfect sense, again, I am very new to all of this.Any help would be appreciated. What I am ultimately trying to achieve is an event tag in google. Thank you.

解决方案

You can use the following code to add the call:

$( "#my_element a" ).bind( "mousedown" , function() {
    _gaq.push( [ "_trackEvent" , "CIGuide" , "Click Shop Size" , "15 3/4 Item" ] ) ;
} ) ;

The code above will work for any anchor under the element whose id is "my_element":

<h1>Hello World</h1>
<div id="my_element">
    <h2>Title here</h2>
    <ul>
        <li>
            <a href="http://www.example.com">Example</a>
        </li>
        <li>
            <a href="./some-page.html">Some Page</a>
        </li>
        <li>
            <a href="http://www.anotherexample.com">Another Example</a>
        </li>
    </ul>
</div>
<a href="./wont-fire">This link won't fire the event</a>

Customize the selector to fit your purposes and that's it.

这篇关于无法将Google Analytics(分析)事件代码成功添加到由.attr创建的链接中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:45