【问题标题】:Appending Title to Google Event Tracking Code将标题附加到 Google 事件跟踪代码
【发布时间】:2014-10-15 05:43:28
【问题描述】:

我在安装了 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,但我担心更新会清除我的代码并迫使我在某个时候重新做。编写一个在顶部捕捉此功能的脚本可能更容易。

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

提前致谢!

【问题讨论】:

    标签: jquery wordpress google-analytics podpress


    【解决方案1】:

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

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

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

    为我工作。希望对您有所帮助!

    【讨论】:

      【解决方案2】:

      你错过了.

      var title = $a.closest('.entry-title').text(); // `.entry-title`
      

      【讨论】:

      • 很好的发现。但是,如果我输入“alert(title);”在那里,我没有看到任何东西出现。我是否正确引用它,因为它位于链接上方的 h2 标记中?
      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 2013-08-14
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多