0
votes

I have a WordPress theme that outputs a portfolio grid based on a shortcode. If you click on the portfolio featured image, you open a modal box with the project details.

I am trying to use google's virtual pageview tracking to log a pageview when a user clicks to open the modal box.

_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);

https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples

Here is how the the portfolio html is generated in my portfolio.php file:

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

I've tried adding the code below below the second line but it is causing an error:

$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

I am assuming I should be formatting that onClick code differently but don't know where to start. From what I can tell, php doesn't like the commas ' necessary in the javascript code. Perhaps I'm way off base as well.

Any help would be appreciated!

1

1 Answers

1
votes

you forgot to escape the quote mark. so php got error

$html .= ' onClick="_gaq.push([\'_trackPageview\', \'/downloads/pdfs/corporateBrief.pdf\']);"';

would work

And i think using this kind of syntax is better than using dot to contact strings.

<?php 
   $google = 'http://www.google.com';
?>
<li>
   <a href="<?php echo $google?>">google</a>
   <!-- works for php5.3+ -->
   <a href="<?= $google ?>">goggle2</a>
</li>

Also you may try some editor that support php code highlight, they will show you the problem