我有一个 WordPress 主题,它根据短代码输出投资组合网格。如果单击投资组合特色图像,您将打开一个包含项目详细信息的模式框。
我正在尝试使用谷歌的虚拟综合浏览量跟踪来记录用户点击打开模式框时的综合浏览量。
_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);
https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples
以下是在我的portfolio.php 文件中生成投资组合 html 的方式:
// GENERATE HTML
$html .= '<li class="item' . $thumb_classes . '" data-title="' . get_the_title() . '">';
$html .= '<a href="' . get_the_permalink() . '"';
$html .= $enable_modal ? ' data-ajax-selector=".portfolio-detail"' : '';
$html .= ' class="item-image';
$html .= $enable_modal ? ' m-open-ajax-modal-project' : '';
$html .= '">' . $thumb_image . '</a>';
$html .= $enable_title || $enable_excerpt ? '<div class="item-info">' : '';
$html .= $enable_title ? '<h4 class="m-secondary-font"><a href="' . get_the_permalink() . '"' : '';
$html .= $enable_title && $enable_modal ? ' data-ajax-selector=".portfolio-detail" class="m-open-ajax-modal-project"' : '';
$html .= $enable_title ? '>' . get_the_title() . '</a></h4>' : '';
$html .= $enable_excerpt ? wpautop( get_the_excerpt() ) : '';
$html .= $enable_title || $enable_excerpt ? '</div>' : '';
$html .= $enable_permalink ? '<div class="item-tools"><a href="' . get_the_permalink() . '"><i class="fa fa-link"></i></a></div>' : '';
$html .= '</li>';
我尝试在第二行下方添加以下代码,但它导致了错误:
$html .= ' onClick="_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);"';
Parse error: syntax error, unexpected '_trackPageview' (T_STRING) in /home/abc/public_html/wp-content/plugins/toolkit/shortcode-generator/shortcodes/portfolio.php on line 234
我假设我应该以不同的方式格式化 onClick 代码,但不知道从哪里开始。据我所知,php 不喜欢 javascript 代码中必需的逗号 ' 。也许我也偏离了基地。
如有任何帮助,我们将不胜感激!
最佳答案
您忘记转义引号。所以 php 出现错误
$html .= ' onClick="_gaq.push([\'_trackPageview\', \'/downloads/pdfs/corporateBrief.pdf\']);"';
可以工作
而且我认为使用这种语法比使用点来联系字符串更好。
<?php
$google = 'http://www.google.com';
?>
<li>
<a href="<?php echo $google?>">google</a>
<!-- works for php5.3+ -->
<a href="<?= $google ?>">goggle2</a>
</li>
你也可以尝试一些支持php代码高亮的编辑器,他们会告诉你问题
关于javascript - 将 JavaScript 与 PHP 混合使用? (跟踪 Google Analytics 虚拟综合浏览量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292332/