_registerListenersForTrackedConditions: function()

in src/apache-unomi-tracker.js [826:891]


        _registerListenersForTrackedConditions: function () {
            console.info('[WEM] Check for tracked conditions and attach related HTML listeners');

            var videoNamesToWatch = [];
            var clickToWatch = [];

            if (wem.cxs.trackedConditions && wem.cxs.trackedConditions.length > 0) {
                for (var i = 0; i < wem.cxs.trackedConditions.length; i++) {
                    switch (wem.cxs.trackedConditions[i].type) {
                        case 'formEventCondition':
                            if (wem.cxs.trackedConditions[i].parameterValues && wem.cxs.trackedConditions[i].parameterValues.formId) {
                                wem.formNamesToWatch.push(wem.cxs.trackedConditions[i].parameterValues.formId);
                            }
                            break;
                        case 'videoViewEventCondition':
                            if (wem.cxs.trackedConditions[i].parameterValues && wem.cxs.trackedConditions[i].parameterValues.videoId) {
                                videoNamesToWatch.push(wem.cxs.trackedConditions[i].parameterValues.videoId);
                            }
                            break;
                        case 'clickOnLinkEventCondition':
                            if (wem.cxs.trackedConditions[i].parameterValues && wem.cxs.trackedConditions[i].parameterValues.itemId) {
                                clickToWatch.push(wem.cxs.trackedConditions[i].parameterValues.itemId);
                            }
                            break;
                    }
                }
            }

            var forms = document.querySelectorAll('form');
            for (var formIndex = 0; formIndex < forms.length; formIndex++) {
                var form = forms.item(formIndex);
                var formName = form.getAttribute('name') ? form.getAttribute('name') : form.getAttribute('id');
                // test attribute data-form-id to not add a listener on FF form
                if (formName && wem.formNamesToWatch.indexOf(formName) > -1 && form.getAttribute('data-form-id') == null) {
                    // add submit listener on form that we need to watch only
                    console.info('[WEM] Watching form ' + formName);
                    form.addEventListener('submit', wem._formSubmitEventListener, true);
                }
            }

            for (var videoIndex = 0; videoIndex < videoNamesToWatch.length; videoIndex++) {
                var videoName = videoNamesToWatch[videoIndex];
                var video = document.getElementById(videoName) || document.getElementById(wem._resolveId(videoName));

                if (video) {
                    video.addEventListener('play', wem.sendVideoEvent);
                    video.addEventListener('ended', wem.sendVideoEvent);
                    console.info('[WEM] Watching video ' + videoName);
                } else {
                    console.warn('[WEM] Unable to watch video ' + videoName + ', video not found in the page');
                }
            }

            for (var clickIndex = 0; clickIndex < clickToWatch.length; clickIndex++) {
                var clickIdName = clickToWatch[clickIndex];
                var click = (document.getElementById(clickIdName) || document.getElementById(wem._resolveId(clickIdName)))
                    ? (document.getElementById(clickIdName) || document.getElementById(wem._resolveId(clickIdName)))
                    : document.getElementsByName(clickIdName)[0];
                if (click) {
                    click.addEventListener('click', wem.sendClickEvent);
                    console.info('[WEM] Watching click ' + clickIdName);
                } else {
                    console.warn('[WEM] Unable to watch click ' + clickIdName + ', element not found in the page');
                }
            }
        },