4 Replies Latest reply on Nov 9, 2017 7:06 AM by arturc85303583

    Custom goal triggering via Javascript

    abbyd77023085

      Is there away somehow i can tirgger a goal via javascript API by writing custom javascript.

      Some thing like

       

      $(".css-class").click(function(){

          //trigger click goal here

         //window.trigger('goal-name');

      });

        • 1. Re: Custom goal triggering via Javascript
          sabahatu1546489 Level 1

          yes thats possible. how have you implemented Adobe Target

           

          via mbox or at.js?

          • 2. Re: Custom goal triggering via Javascript
            abbyd77023085 Level 1

            Hi sabahatu,

             

            Thanks for replying back.

             

            As i didn’t setup the target for the website. I cannot say for sure.

            Client has already set it up. I got the access later.

             

            I think its at.js,

            cuz the elements in page have class like “at-element-marker”

            and also “at-element-click-tracking” (when click goal is added via “Goal & Settings”)

             

            But when i check the Setup=> Implementation, mbox.js is selected.

             

            To add up,

            I have already tried following

            <script>

            $('a').click(function(){

              adobe.target.trackEvent({'mbox’:’quicklinkclick’})

            });

            </script>

             

            and added conversion goal to view mbox “quicklinkclick”;

            when i check console log in chrome, i got following:

            VM768 satellite-59b9eb0164746d4b1b00b18b.js:18

            AT: Track event request failed {mbox: "quicklinkclick", params: {…}, timeout: 15000, success: ƒ, error: ƒ} {status: "error", error: "abort"}

            • 3. Re: Custom goal triggering via Javascript
              sabahatu1546489 Level 1

              you are on a right track.
              I assume you first created this mbox "quicklinkclick" through console and secondly under metrics you selected "viewed an mbox" (Not clicked on mbox)

              cheers,

              Sabahat

              • 4. Re: Custom goal triggering via Javascript
                arturc85303583 Level 1

                abbyd77023085, modern browsers like Chrome and FireFox try to optimize resource usage as much as possible, so when we use code like this:

                <script>
                $('a').click(function(){
                  adobe.target.trackEvent({mbox:'quicklinkclick'})
                });
                </script>
                

                What happens behind the scenes is the following:

                - User clicks the link
                - Click event listener is executed and a click tracking request is fired
                - At the same time browser sees that he has to load a new page, because user clicked on a link, so Chrome and FireFox will "cancel" the click tracking request and that's why we see the "abort" message being logged

                 

                To fix this we'll have to do the following:

                - Prevent default click handling

                - Load the page referenced by the link manually

                 

                The code could look like this:

                <script>
                $('a').click(function(e){
                  e.preventDefault();
                
                  adobe.target.trackEvent({
                    mbox:'quicklinkclick',
                    success: function() {
                      location.href = $(a).attr('href');
                    },
                    error: function() {
                      location.href = $(a).attr('href');
                    },
                    timeout: 500
                  });
                });
                </script>
                

                NOTE: it is important to use a timeout, so we don't block the page navigation for too long. Also it is important to use success and error callbacks to make sure that we navigate away ONLY when Target request succeeded or failed.

                1 person found this helpful