javascript - How to Track a Google Analytics Custom Event on an Embedded Form? -
i have form embedded on site via javascript. how can track custom event in google analytics when form submitted?
i have form them embedded in site including javascript tag, following:
<script src="https://app.convertkit.com/landing_pages/531.js?orient=horz"></script>
where tag included, html injected onto page. here example:
<div class="ck_embed_form ck_horizontal_subscription_form"> <div class="ck_embed_form_content"> <h3 class="ck_embed_form_title">freelance pricing handbook</h3> <div class="ck_embed_description"> <span class="ck_image"> <img alt="book-portrait" src="http://s3.amazonaws.com/convertkit/subscription_forms/images/004/811/858/standard/book-portrait.png?1398265358"> </span> <p>are confused how should price services? maybe worried charging clients or little. i'll find perfect price business.</p> </div> </div> <div id="ck_success_msg" style="display:none;"> <p>thanks! check email.</p> </div> <!-- form starts here --> <form id="ck_subscribe_form" class="ck_subscribe_form" action="https://app.convertkit.com/landing_pages/531/subscribe" data-remote="true"> <input type="hidden" name="id" value="531" id="landing_page_id"> <p class="ck_errorarea"></p> <div class="ck_control_group"> <label class="ck_label" for="ck_firstnamefield">first name</label> <input type="text" name="first_name" class="ck_first_name" id="ck_firstnamefield" required=""> </div> <div class="ck_control_group"> <label class="ck_label" for="ck_emailfield">email address</label> <input type="email" name="email" class="ck_email_address" id="ck_emailfield" required=""> </div> <label class="ck_checkbox" style="display:none;"> <input class="optin ck_course_opted" name="course_opted" type="checkbox" id="optin" checked=""> i'd receive free 30 day course email. </label> <button class="subscribe_button ck_subscribe_button btn fields" id="ck_subscribe_button"> free book </button> <span class="ck_guarantee">we won't send spam. unsubscribe @ time.</span> </form> </div>
here attempt @ collecting event in google analytics. event handler not firing when form submitted:
$("#ck_subscribe_form").on('submit', function(e) { console.log('testing google anayltics custom event'); _gaq.push(['_trackevent', 'newsletter', 'subscribe', 'freelance pricing handbook']); });
how can track event?
you can see live example of here: freelance pricing handbook custom tracking code isn't live until can squared away.
make sure jquery library supports on function (1.7+) , you're attaching event after dom ready using document ready syntax. following worked fine me:
<script src="https://app.convertkit.com/landing_pages/531.js?orient=horz"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type='text/javascript'> $(function(){ $('#ck_subscribe_form').on('submit',function(){ console.log('testing google anayltics custom event'); _gaq.push(['_trackevent', 'newsletter', 'subscribe', 'freelance pricing handbook']); }); }); </script>
Comments
Post a Comment