Skip to content

Instantly share code, notes, and snippets.

@chavesm
Last active September 17, 2020 10:16
Show Gist options
  • Save chavesm/472b728ac0fa044ddc4420d1209e5d5a to your computer and use it in GitHub Desktop.
Save chavesm/472b728ac0fa044ddc4420d1209e5d5a to your computer and use it in GitHub Desktop.
Manually send a conversion hit to GA when a submit button is clicked on a form.
<form
action="http://caughtmyeye.epizy.com/simple-form/"
id="simple-test-form"
method="post"
data-avia-form-id="1"
data-avia-redirect=""
_lpchecked="1"
>
<fieldset>
<h3>Simple Test Form</h3>
<p id="element_avia_1_1">
<label for="avia_1_1"
>Name <abbr title="required">*</abbr></label
>
<input
name="avia_1_1"
type="text"
id="avia_1_1"
value=""
/>
</p>
<p
id="element_avia_2_1"
>
<label for="avia_2_1"
>Message <abbr title="required">*</abbr></label
>
<textarea
name="avia_2_1"
cols="40"
rows="7"
id="avia_2_1"
></textarea>
</p>
<p style="display:none;">
<input
type="text"
name="avia_3_1"
id="avia_3_1"
value=""
/>
</p>
<p>
<input type="hidden" value="1" name="avia_generated_form1" />
<input
id="simple-test-form-btn"
type="submit"
value="Submit"
class="button"
data-sending-label="Sending"
/>
</p>
</fieldset>
</form>
if ( document.URL === 'http://caughtmyeye.epizy.com/simple-form/') {
const subBtn= document.getElementById(
"simple-test-form-btn"
);
if (subBtn) {
subBtn.addEventListener(
"click",
function(evt) { ga("send", "event", "form", "conversion", "Simple Form Test Submit", 1) },
false
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment