• Resolved wouterdoorn

    (@wouterdoorn)


    Hi,

    first of all, thanks for the great plugin!
    I am trying to keep track of submitted contact forms via Google Analytics for conversion analysis but can’t get that to work. I tried the following:

    html_forms.on(‘success’, function(formElement) {
    ga( ‘send’, ‘event’, ‘Contact Form’, ‘submit’ );
    })

    but get a Uncaught ReferenceError: html_forms is not defined at… error

    Any tips on how to get it to work?
    I guess I could also forward visitors who succesfully submited a form to https://domain.tld/contactform/#succes or something like that an make Analytics track visits to the #succes url, but that seem somewhat roundabout.

    Best,

    Wouter

Viewing 3 replies - 1 through 3 (of 3 total)
  • That means your code is running before HTML Forms scripts are loaded.
    You can try a document.addEventListener(‘DOMContentLoaded’, function {…}, false); around it or move the code to the end of the document.

    Hope that helps. If you have any questions, please let me know!

    Thread Starter wouterdoorn

    (@wouterdoorn)

    Hmm… I got the error to disapper with:

    document.addEventListener('DOMContentLoaded', function () {
    html_forms.on('success', function(formElement) {
       ga( 'send', 'event', 'Contact Form', 'submit' );
    })
    }, false)

    and then only loading that script on the pages where the form is present via functions.php

    But now, submitting acontact form stopped working, I just get the following error:

    Uncaught ReferenceError: ga is not defined
        at 40961.js?de585:1
        at d770d.js?07e2c:1
        at Array.forEach (<anonymous>)
        at Object.trigger (d770d.js?07e2c:1)
        at s (d770d.js?07e2c:1)
        at XMLHttpRequest.r.onreadystatechange (d770d.js?07e2c:1)

    I am not that code savy. I thought the ga() code would work but clearly it doesn’t. Any ideas?

    Either you are using another type of Google Analytics script (instead of ga(..)) or the GA script is deferred loading after DomContentLoaded.

    Hope that helps. If you have any questions, please let me know!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Google Analytics tracking’ is closed to new replies.