Issue
I'm trying to activate onclick
on a div.
For some reason it's not working.
Example:
<html>
<head>
<style>
.box{
border: 1px solid black;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box" onclick="_gaq.push(['_trackEvent', 'Click', 'Facebook_Share', 'Upper_Button']);">
</div>
</body>
</html>
Because for some reason it's not working.
Am I writing it wrong?
I wrote this code as an example. The JS is in the real codes <head>
.
(It's a Google Analytics event... )
Solution
You should bind the onclick element to the box div after you have loaded the Google Analytics script:
<html>
<head>
<style>
.box{
border: 1px solid black;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box" id="tracked">
</div>
<!-- suggest using jQuery library, as it eases things, see below -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- put Google Analytics code here (I assume, the _gaq variable is the Google Analytics one) -->
<script type="text/javascript"><!-- replace this comment with your Google Analytics code --></script>
<script type="text/javascript">
$(document).ready(function(){
// bind click event to the box div (I used an id here)
$("#tracked").click(function(){
_gaq.push(['_trackEvent', 'Click', 'Facebook_Share', 'Upper_Button']);
});
});
</script>
</body>
</html>
Answered By - beerwin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.