admin管理员组文章数量:1435859
I'm looking to add a Google Analytics event to a form that I can not access the inline html, so I can not add it as a onClick=""
event straight to the html.
So my solution has been so far:
$(function() {
$(".form_submit input").on("click", function() {
dataLayer.push({
"event": "Kontakt",
"eventCategory": "Submit",
"eventAction": "Kirjuta meile",
"eventLabel": "Kirjuta meile"
});
});
});
Althought this does not seem to work as clicking the submit button possibly stops all functions and refreshes the page.
How can I run the function before submit and then submit the form after? I've been suggested using preventDefault();
and the after calling the submit again with $('form').one('submit', ...
but have been unable to implement this due to lack of skill.
View site: (the form is at the bottom of the page)
Any suggestions appreciated.
I'm looking to add a Google Analytics event to a form that I can not access the inline html, so I can not add it as a onClick=""
event straight to the html.
So my solution has been so far:
$(function() {
$(".form_submit input").on("click", function() {
dataLayer.push({
"event": "Kontakt",
"eventCategory": "Submit",
"eventAction": "Kirjuta meile",
"eventLabel": "Kirjuta meile"
});
});
});
Althought this does not seem to work as clicking the submit button possibly stops all functions and refreshes the page.
How can I run the function before submit and then submit the form after? I've been suggested using preventDefault();
and the after calling the submit again with $('form').one('submit', ...
but have been unable to implement this due to lack of skill.
View site: http://avrame./en (the form is at the bottom of the page)
Any suggestions appreciated.
Share Improve this question asked May 8, 2017 at 9:12 user3615851user3615851 9901 gold badge15 silver badges44 bronze badges 1- Are you using Google tag manager? Your code seems to suggest do. Can you post screenshots of your associated tags and triggers? – nyuen Commented May 8, 2017 at 13:23
2 Answers
Reset to default 3You can actually push functions to dataLayer
, and it will be executed after the first event.
I would do
- delegate the submit watch event to document level (see Jquery .on() submit event)
- intercept the first submit, pushing event and preventing default behavior
- and insert a function inside
dataLayer
, which submits the form again, but this time it won't be halted
The code:
window.submitGA = false;
$(function() {
$(document).on('submit','.form_submit',function(event){
if (!window.submitGA)
{
window.submitGA = true;
dataLayer.push({
"event": "Kontakt",
"eventCategory": "Submit",
"eventAction": "Kirjuta meile",
"eventLabel": "Kirjuta meile"
});
dataLayer.push(function(){
$('.form_submit').submit();
});
event.preventDefault();
}
});
});
Working solution ended up using this callback method:
var form = document.getElementsByClassName('.footer__contact form');
form.addEventListener('submit', function(event) {
event.preventDefault();
setTimeout(submitForm, 1000);
var formSubmitted = false;
function submitForm() {
if (!formSubmitted) {
formSubmitted = true;
form.submit();
}
}
ga('send', 'event', 'submit', 'Saada', 'Kirjuta meile', {
hitCallback: submitForm
});
});
Reference from: https://developers.google./analytics/devguides/collection/analyticsjs/sending-hits#hitcallback
本文标签: javascriptAdding Google Analytics event to form submitStack Overflow
版权声明:本文标题:javascript - Adding Google Analytics event to form submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745642196a2667925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论