Create shortcode to insert CTA button with with javascript event tracking

Viewed 145

I want shortcode to insert a CTA button that says "Request a Quote", but also want it to include onClick event tracking code with the current page title.

This code breaks WordPress (I think it's a problem with single and double quotes)

function my_cta() {
    $title = echo get_the_title();
    return '<div class="mycalltoaction"><a class="button" href="/contact-us/" onClick="_gaq.push(['_trackEvent', 'QuoteRequest', 'Click', '$title']);">Request a Quote <strong>TODAY</strong></a></div>';
}
add_shortcode('cta', 'my_cta');

What am I doing wrong?

1 Answers

You got syntax error on some of your code. I already got it fixed.

<?php
function my_cta() {
    return `<div class="mycalltoaction"><a class="button" href="/contact-us/" onClick="_gaq.push(['_trackEvent', 'QuoteRequest', 'Click', '`.get_the_title().`']);">Request a Quote <strong>TODAY</strong></a></div>`;
}
add_shortcode('cta', 'my_cta');
  1. is on the $title = echo get_the_title(); you should not assign echo to a variable.
  2. you combined string with the variable without closing the string. (['_trackEvent', 'QuoteRequest', 'Click', '$title']);
Related