• Resolved ricky1191

    (@ricky1191)


    Hello,

    I use Yikes Mailchimp form in my wordpress account. How do I track the form submissions in google analytics? I checked this article https://yikesplugins.com/support/knowledge-base/add-google-analytics-event-tracking/.

    Where should the below code be inserted? I use google tag manager as well. Can this entire code be added in GTM tag? Please explain the procedure.

    add_action( ‘yikes-mailchimp-google-analytics’, ‘yikes_mailchimp_google_analytics’, 10, 1 );

    function yikes_mailchimp_google_analytics( $form_id ) {
    ?>
    <script type=”text/javascript”>

    var form_id = <?php echo $form_id; ?>;

    // Fire off GA event for a failed subscription
    function yikes_mailchimp_google_analytics_failure( response ) {

    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,’script’,’https://www.google-analytics.com/analytics.js&#8217;,’ga’);

    ga( ‘create’, ‘UA-xxxxx-1’, ‘auto’ );
    ga( ‘send’, ‘event’, ‘mailchimp-subscribe’, ‘subscription-failed’ );
    }

    // Fire off GA event for a successful subscription
    function yikes_mailchimp_google_analytics_success( response ) {

    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,’script’,’https://www.google-analytics.com/analytics.js&#8217;,’ga’);

    ga( ‘create’, ‘UA-xxxxx-1’, ‘auto’ );
    ga( ‘send’, ‘event’, ‘mailchimp-subscribe’, ‘subscription-successful’ );
    }

    </script>
    <?php
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @ricky1191,

    The code you are referencing is PHP, so you will not be able to add it to GTM. GTM manages the loading of javascript.

    Essentially, the code is firing custom Google Analytics events when certain actions occur. You have to define the functions in PHP in order to pass the form id to the script.

    If you are using GTM to load the google analytics script, you can remove parts of that code:

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    				(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    				m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    				})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    
    				ga( 'create', 'UA-xxxxx-1', 'auto' );
    

    That is the part that loads Google Analytics but GTM should be set up to do that itself.

    After removing that section from both functions in the code you sent, you’ll need to add the remainder of the code to your functions.php file or similar.

    Let me know if you have any questions,
    Jon

    I should also mention that if you’d prefer to do this the default way (without GTM), you just need to change the value of UA-xxxxx-1 and add the whole section to your functions file.

    If you aren’t comfortable editing the PHP files, you can use a plugin such as https://wordpress.org/plugins/my-custom-functions/

    Jon

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

The topic ‘Google Anlaytics Goal Tracking’ is closed to new replies.