admin管理员组文章数量:1415111
I want to track on how many people is clicking the button and sessions will be collected in Google Analytics. However, the I cannot get any result in Google Analytics Events. My code is below:
<script>
function handleOutboundLinkClicks(event) {
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: event.target.href,
transport: 'beacon'
});
}
</script>
//// The code for my button is below:
<input type="submit" id="submit" value="Validate and Submit" onclick=”_gaq.push([‘_trackEvent’, ‘form, ‘submit’, ’order sent’, 10]);”/>
Then entire code for this page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Keepsake Florals</title>
<!--link to external css file-->
<link rel="stylesheet" type="text/css" href="keepsakeflorals.css"/>
<link rel="stylesheet" type="text/css" href="ksform.css" />
<link rel="shortcut icon" href="favicon.ico" />
<script src="scripts/jquery-1.11.3.min.js"></script>
<script>
function handleOutboundLinkClicks(event) {
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: event.target.href,
transport: 'beacon'
});
}
</script>
</head>
<body>
<div id="box">
<h1><a href="index.html" title="click here to return home"> KEEPSAKE FLORALS </a> </h1>
</div>
<div>
<nav id="mainnav">
<a href="index.html"> Home</a> |
<a href="aboutus.html"> About Us </a> |
<a href="flowers.html"> Flowers </a> |
<a href="order.html"> Order </a> |
<a href="review.html"> Review </a> |
</nav>
</div>
<form action="thankyou.html" id="contact-form">
<fieldset id="contactinfo">
<legend> <span> Contact Information </span> </legend>
<label>
Name
<input type="text" name="name" id="nameinput" placeholder="First and last name" />
</label>
<label>
Email
<input type="email" name="email" id="emailinput" placeholder="[email protected]" />
</label>
<label>
Phone
<input type="text" name="phone" id="phoneinput" placeholder="phone number" />
</label>
</fieldset>
<fieldset id="orderflower">
<legend> <span> Flowers to order </span> </legend>
<label for="mother" >
<input type="radio" name="flower" id="mother" value="mother" checked="checked" />
Mother's day flower
</label>
<label for="father">
<input type="radio" name="flower" id="father" value="father" />
Fathers day flower
</label>
<label for="otheroccasion">
<input type="radio" name="flower" id="occasion" value="occasion" />
Other occasion flower
</label>
<label for="valentinesday">
<input type="radio" name="flower" id="vday" value="vday" />
Valentine's Day
</label>
<label for="others">
<input type="radio" name="flower" id="others" value="others" />
Others
<input type="text" name="other" id="otherinput">
</label>
</fieldset>
<fieldset id="delivery">
<legend> <span> How to contact you ? </span> </legend>
<label for "question">
<input type="radio" name="flower" id="phone" />
Via my handphone number
</label>
<label for "question">
<input type="radio" name="flower" id="email" />
Via my email
</label>
</fieldset>
<fieldset id="helpneeded" >
<legend> <span> Any help needed? </span> </legend>
<textarea id="helpneeded" name="helpneeded" rows="4" cols="20"> </textarea>
</fieldset>
<input type="submit" id="submit" value="Validate and Submit" onclick=”ga('send', 'event', 'form', 'submit', 'order sent', 10);”/>
</form>
</body>
</html>
I want to track on how many people is clicking the button and sessions will be collected in Google Analytics. However, the I cannot get any result in Google Analytics Events. My code is below:
<script>
function handleOutboundLinkClicks(event) {
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: event.target.href,
transport: 'beacon'
});
}
</script>
//// The code for my button is below:
<input type="submit" id="submit" value="Validate and Submit" onclick=”_gaq.push([‘_trackEvent’, ‘form, ‘submit’, ’order sent’, 10]);”/>
Then entire code for this page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Keepsake Florals</title>
<!--link to external css file-->
<link rel="stylesheet" type="text/css" href="keepsakeflorals.css"/>
<link rel="stylesheet" type="text/css" href="ksform.css" />
<link rel="shortcut icon" href="favicon.ico" />
<script src="scripts/jquery-1.11.3.min.js"></script>
<script>
function handleOutboundLinkClicks(event) {
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: event.target.href,
transport: 'beacon'
});
}
</script>
</head>
<body>
<div id="box">
<h1><a href="index.html" title="click here to return home"> KEEPSAKE FLORALS </a> </h1>
</div>
<div>
<nav id="mainnav">
<a href="index.html"> Home</a> |
<a href="aboutus.html"> About Us </a> |
<a href="flowers.html"> Flowers </a> |
<a href="order.html"> Order </a> |
<a href="review.html"> Review </a> |
</nav>
</div>
<form action="thankyou.html" id="contact-form">
<fieldset id="contactinfo">
<legend> <span> Contact Information </span> </legend>
<label>
Name
<input type="text" name="name" id="nameinput" placeholder="First and last name" />
</label>
<label>
Email
<input type="email" name="email" id="emailinput" placeholder="[email protected]" />
</label>
<label>
Phone
<input type="text" name="phone" id="phoneinput" placeholder="phone number" />
</label>
</fieldset>
<fieldset id="orderflower">
<legend> <span> Flowers to order </span> </legend>
<label for="mother" >
<input type="radio" name="flower" id="mother" value="mother" checked="checked" />
Mother's day flower
</label>
<label for="father">
<input type="radio" name="flower" id="father" value="father" />
Fathers day flower
</label>
<label for="otheroccasion">
<input type="radio" name="flower" id="occasion" value="occasion" />
Other occasion flower
</label>
<label for="valentinesday">
<input type="radio" name="flower" id="vday" value="vday" />
Valentine's Day
</label>
<label for="others">
<input type="radio" name="flower" id="others" value="others" />
Others
<input type="text" name="other" id="otherinput">
</label>
</fieldset>
<fieldset id="delivery">
<legend> <span> How to contact you ? </span> </legend>
<label for "question">
<input type="radio" name="flower" id="phone" />
Via my handphone number
</label>
<label for "question">
<input type="radio" name="flower" id="email" />
Via my email
</label>
</fieldset>
<fieldset id="helpneeded" >
<legend> <span> Any help needed? </span> </legend>
<textarea id="helpneeded" name="helpneeded" rows="4" cols="20"> </textarea>
</fieldset>
<input type="submit" id="submit" value="Validate and Submit" onclick=”ga('send', 'event', 'form', 'submit', 'order sent', 10);”/>
</form>
</body>
</html>
Share
Improve this question
edited Jan 28, 2017 at 6:46
xhinvis
asked Jan 25, 2017 at 16:34
xhinvisxhinvis
2114 silver badges15 bronze badges
1
- Possible duplicate of Track event in google analytics upon clicking form submit – xhinvis Commented Jan 31, 2017 at 0:16
2 Answers
Reset to default 2There are a couple of things wrong:
You're mixing the deprecated classic GA syntax with the new Universal Analytics syntax. UA does not use the
_gaq
object anymore. Youronclick
code should be:ga('send', 'event', 'form', 'submit', 'order sent', 10);
Note that you also had a mix of smart quote and straight quotes. You should just use straight quotes.
You've defined a function
handleOutboundLinkClicks
that doesn't seem to correlate with the button, so you won't see theOutbound link
event when you click your button.
But for all intents and purposes, the code I posted should work for your button, if that's your main concern.
This is a bit late, but I hope this would help someone who lands on this question.
Your issue is most likely because the Google Analytics Javascript code stops execution once your form gets submitted and loads a new page (thankyou.html
). You have two options:
- Use the
transport
property and set its value tobeacon
:
ga('send', 'event', 'form', 'submit', 'order sent', 10, {
transport: 'beacon'
});
- For browsers that do not support the
beacon
transport method, you can use a callback function:
In your HTML, modify your submit button (remove the onclick
attribute):
<input type="submit" id="submit" value="Validate and Submit" />
Then, add an event listener to this submit button:
var form = document.getElementById('contact-form');
form.addEventListener('submit', function(e) {
e.preventDefault(); //prevent the form from submitting immediately
//Send the event to Google Analytics, and then manually
//submit the form
ga('send', 'event', 'form', 'submit', 'order sent', 10, {
hitCallback: function() {
form.submit(); //submit the form now
}
});
});
References:
- Outbound link and form tracking
- Knowing when the hit has been sent
本文标签: javascriptGoogle Analytics Event Tracking through onclick buttonStack Overflow
版权声明:本文标题:javascript - Google Analytics Event Tracking through onclick button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745216466a2648194.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论