jquery - 将标题附加到 Google 事件跟踪代码

标签 jquery wordpress google-analytics podpress

我在安装了 Thesis 的 Wordpress 上使用 Podpress 运行播客。目前,Podpress 有自己的内部统计系统来跟踪播放和下载,但我想通过在听众点击下载/播放/任何媒体文件时添加一些 Google 事件跟踪来扩展它。下面,我包含了一个我们的系统为新条目生成的 HTML 代码示例:

<div class="teasers_box">

            <div id="post-2855" class="post-2855 post type-post status-publish format-standard hentry category-episodes teaser">

<h2 class="entry-title"><a title="Permanent link to Show Title" rel="bookmark" href="http://www.test.com/2012/02/22/show-title/">Show Title</a></h2>
<abbr title="2012-02-22" class="teaser_date published">February 22, 2012</abbr>

<div class="format_teaser entry-content">
<p>Show Description</p>
<div class="podPress_content podPress_content_audio_mp3">
<div style="display:block;" class="podpress_playerspace podpress_playerspace_audio_mp3 podpress_mp3player"></div>

<div class="podPress_downloadlinks podPress_downloadlinks_audio_mp3"><a class="podpress_downloadimglink podpress_downloadimglink_audio_mp3" title="Download: Show Title" target="new" href="http://www.test.com/podpress_trac/web/2855/0/2012_02_22_showtitle.mp3"><img alt="" class="podPress_imgicon podpress_imgicon_audio_mp3" src="http://www.test.com/wp-content/plugins/podpress/images/audio_mp3_button.png"></a> <span class="podpress_mediafile_title podpress_mediafile_title_audio_mp3">Show Title</span> <span class="podpress_mediafile_dursize podpress_mediafile_dursize_audio_mp3">[ 39:21 ]</span> <a class="podpress_downloadlink podpress_downloadlink_audio_mp3" target="new" href="">Download</a></div></div>
</div>

我有一些 jQuery 代码,一些好心人在此板上早些时候帮助我。我一直在修补它,试图弄清楚我想要什么。现在,这就是我所拥有的:
$('a.podpress_downloadlink_audio_mp3').click(function(e) {
     e.preventDefault();
      var $a = $(this);
      //  $a is the anchor that was clicked. you can access attributes or its  text to populate the _gaq.push call below.  e.g. var text = $a.text();

     var title = $a.closest('entry-title').text();
     _gaq.push(['_trackEvent', 'Podcasts', 'Download', title]);
    });

基本上,我的问题是我需要对 DIV 进行排序,以便获取可以传递给 Google Analytics 的播客剧集的标题(在本例中,它只是“显示标题”)。我正在尝试使用 .closest() 技术来做到这一点,但我认为我使用不正确。虽然我想进入并实际修改 Podpress 插件背后的 PHP,但我担心更新会清除我的代码并迫使我在某个时候重新做。编写一个在顶部捕捉此功能的脚本可能更容易。

任何有关如何获得此头衔的建议将不胜感激。我目前的努力似乎失败了。

提前致谢!

最佳答案

我知道您不更改模块中的 PHP 是什么意思,您总是希望尽可能避免这种情况。

不知道您的 html 在多个播客节点上的外观如何,您可以试试这个 - http://jsfiddle.net/CcdDA/

var title = $a.parents('.format_teaser').siblings().prev('h2.entry-title').text();

为我工作。希望能帮助到你!

关于jquery - 将标题附加到 Google 事件跟踪代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509060/

相关文章:

javascript - 在 mouseleave 上调用clearInterval

javascript - 使用 JQuery 或 Js (MVC) 禁用更新表单中的“保存”按钮

javascript - 如何检查表单中是否单击了动态生成的单选按钮?

heroku - 导轨 5 : How can I get Google Analytics to work?

javascript - 如何以编程方式更新与 Google Analytics 中的客户端 ID 关联的自定义字段?

angularjs - 检测用户是否使用 google chrome 的自动填充选项填写表单

javascript - jQuery 滚动时折叠

css - 我无法将按钮居中

html - 将 ul 列表与跨度对齐?

php - 类别 ID 上的 query_posts 不起作用