• Resolved cemba

    (@cemba)


    Hey,

    i want to push datalayer to gtm but i tried few method and didnt help me much. i just want to push some data from input value with datalayer.

    sample codes;

    first one normal function for submit button when user click(and this one getting empty datalayer[]);

    function dtGelisim(){
      var statuse = document.querySelectorAll("#statusId li.active a span")[0].innerText;
      
        var today = new Date();
        var selectedday = new Date(document.getElementsByName("BDAY")[0].value);
        var oneDay = 24 * 60 * 60 * 1000;
        var diffDays = Math.round(Math.abs((selectedday - today) / oneDay));
        
        dataLayer.push({
        'event': 'FormSubmit',
        'MainCategory': 'xx',
        'SubCategory': 'xx',
        'Status': statuse,
        'RemainingDay': diffDays,
        'Kvkk': 'Yes'
        });
        console.log(dataLayer);
     }

    second i found on your document but this didnt work well, after user click and form submitted page reload and form data gone cant get date or something.

    mc4wp.forms.on('submitted', function(form) {
    var statuse = document.querySelectorAll("#statusId li.active a span")[0].innerText;	  
    var today = new Date();
    var selectedday = new Date(document.getElementsByName("BDAY")[0].value);
    var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
    var diffDays = Math.round(Math.abs((selectedday - today) / oneDay));
    
    dataLayer.push({ 'event' : 'FormSubmit', 'MainCategory' : 'xx', 'SubCategory': 'xx', 'Status': statuse, 'RemainingDay': diffDays, 'Kvkk': 'Yes'  });
    console.log(dataLayer);
    });
    • This topic was modified 5 years, 2 months ago by cemba.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Lap

    (@lapzor)

    do you see the console log(datalayer) in your browser console?
    any JS error in the browser console?
    do you use dataLayer.push on other places in your site, and it works there?

    Thread Starter cemba

    (@cemba)

    do you see the console log(datalayer) in your browser console?
    Yes i see datalayer on console log but empty array

    any JS error in the browser console?
    no jserror

    do you use dataLayer.push on other places in your site, and it works there?
    yep i used it for multiple trigger form and it worked.

    used this function type for other forms;

    function dtGelisim(){
      var statuse = document.querySelectorAll("#statusId li.active a span")[0].innerText;
      
        var today = new Date();
        var selectedday = new Date(document.getElementsByName("BDAY")[0].value);
        var oneDay = 24 * 60 * 60 * 1000;
        var diffDays = Math.round(Math.abs((selectedday - today) / oneDay));
        
        dataLayer.push({
        'event': 'FormSubmit',
        'MainCategory': 'xx',
        'SubCategory': 'xx',
        'Status': statuse,
        'RemainingDay': diffDays,
        'Kvkk': 'Yes'
        });
        console.log(dataLayer);
     }
    Plugin Contributor Lap

    (@lapzor)

    Hi,

    If it’s an empty array in your console we can conclude that the
    mc4wp.forms.on(‘submitted’, function(form) { }
    is correctly executed but that there is some issue with the dataLayer object you are setting up there. I recommend to console log each parameter or to experiement with executing the dataLayer.push command with expected variables manually in the bowser console.

    Kind regards

    Thread Starter cemba

    (@cemba)

    mc4wp.forms.on(‘submitted’, function(form) { } if i use this function it ll work after page reload not onclick, i cant get input value correct. how can i disable pagereload.

    Plugin Contributor Harish Chouhan

    (@hchouhan)

    Hey @cemba,

    Disabling Page reload is not possible since the free version does not have Ajax submission. For the form to be processed the page needs to reload.

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

The topic ‘datalayer push with form input value’ is closed to new replies.