简体   繁体   中英

Google Analytics Not Tracking Event

I have the following script between the head tags.

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xxxxxx-2', 'mywebsite.com');
ga('send', 'pageview');
</script>  

then i have the following script in the body with the necessary //Server Connect. Trying to pull the "event" into analytics. Getting firm_website, firm_name, firm_location, and firm_slogan and is pulling the necessary information into their pages from the database. but, having issues actually getting the event to appear in analytics. We would have to update 2,000 plus pages and that is really not an option. thanks for the help. I'm a little knowledgeable when it comes to php and need help coding the below.

<?php
//Display Firm Text
echo '<p class="alignCenter"><font size="+1" color="#930"><a target="_blank" href="http://www.' . $firm_website . '" onclick="ga('send','event','Outgoing Links','. $firm_website .')"><strong>' . $firm_name . '</strong></a></font><br />';
echo '' . $firm_location . '<br />';
echo '<i>' . $firm_slogan . '</i></p>';
?>

I think the issue may come from the use of single quotes and double quotes. You are using single quotes to define your php string, and double quotes in your HTML, which is ok. However, in the ga function, you are using single quotes where you should be usin double quotes I think. I would try something like the below:

<?php
//Display Firm Text
echo '<p class="alignCenter"><font size="+1" color="#930"><a target="_blank" href="http://www.' . $firm_website . '" onclick="ga("send","event","Outgoing Links","'. $firm_website .'")"><strong>' . $firm_name . '</strong></a></font><br />';
echo '' . $firm_location . '<br />';
echo '<i>' . $firm_slogan . '</i></p>';
?>

Like Greg, in your code :

onclick="ga('send','event','Outgoing Links','. $firm_website .')"

Will give you that :

onclick="ga('send','event','Outgoing Links',www.site.com)"

So, you forget the ' :

onclick="ga('send','event','Outgoing Links',\''. $firm_website .'\')"

Here is the correct code below - Tested and is working as expected.

<?php
//Display Firm Text
echo '<p class="alignCenter"><font size="+1" color="#930"><a target="_blank" href="http://www.' . $firm_website . '" onclick="ga(\'send\',\'event\',\'Outgoing Links\',\''. $firm_website .'\')" <strong>' . $firm_name . '</strong></a></font><br />';
echo '' . $firm_location . '<br />';
echo ('<i>' . $firm_slogan . '</i></p>');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM