Chaim Leichman
Chaim Leichman

Reputation: 118

Form submit after GTM dataLayer push

I am using GTM's Enhanced Ecommerce to report events via pushes to the dataLayer variable.

I have a situation where I need to push an event and immediately submit a form in the event callback. Google in theory provides the eventCallback mechanism for this, but consider the following code:

<script>
dataLayer.push({
  'ecommerce': {
    'currencyCode': 'EUR',
    'impressions': [
     {
       'name': 'Donut Friday Scented T-Shirt',
       'id': '67890',
       'price': '33.75',
       'brand': 'Google',
       'category': 'Apparel',
       'variant': 'Black',
       'list': 'Search Results',
       'position': 2
     }]
  }
},{
    'eventCallback': function() {
        document.location = productObj.url; // works
        document.getElementById('form-id').submit(); // fails (!?!)
    }
});
</script>

Notes:

  1. There no Javascript errors on the page
  2. The form #form-id exists (not a matter of typos)

Bottom line: how do I go about causing the form to submit after the push? Or is this not possible?

Upvotes: 2

Views: 2571

Answers (1)

rgcalsaverini
rgcalsaverini

Reputation: 675

Any particular reason as for why you are using GTM to submit a form (particularly after a product impression)? This is probably beyond its intended use and sounds way too hacky.

Assuming the website does the push itself, you could submit the form immediately after the dataLayer.push on the webpage script, and using GTM's own form submit delay catch the push, assemble the product impression tag yourself, then let the form submit as expected after it fired. That should do it.

Upvotes: 1

Related Questions