Trigger email on click of a link in webpage using campaign | Community
Skip to main content
New Participant
April 16, 2020
Solved

Trigger email on click of a link in webpage using campaign

  • April 16, 2020
  • 1 reply
  • 4206 views

Hi Experts,

I have injected munchkins js in my webpage using following code:

<script type="text/javascript"> (function() { var didInit = false; function initMunchkin() { if(didInit === false) { didInit = true; Munchkin.init('token-AAA'); } } var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src='//munchkin.marketo.net/munchkin.js'; s.onreadystatechange = function() { if (this.readyState == 'complete' || this.readyState == 'loaded') { initMunchkin(); } }; s.onload = initMunchkin; document.getElementsByTagName('head')[0].appendChild(s); document.onclick = function(event) { event = event || window.event; // IE specials var target = event.target; if(target.nodeName === "A") { var urlThatWasClicked = 'https://console.someurl.com/dashboard'; Munchkin.munchkinFunction("visitWebPage", { url : urlThatWasClicked }); } }; })(); </script>

 

On Click of the link, I want to send email using campaign.

I created a campaign and following is the smart list configuration:

and following is the flow configuration:

In the scheduled tab I have activated the campaign.

Now as soon I link on the link in my webpage, email should be sent to the email ID specified in "To Other Emails" but it's not working. Am I missing anything here?

Really appreciate your inputs on this.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

Thanks for making that change.

 

There are several problems with your code.

 

  1.  No need to insert your custom listener inside the standard Munchkin init routine. Keep it in a separate function or else it can easily be replaced by someone updating the embed code.
  2. You don't need the legacy event || window.event, I doubt your site actually supports IE 8.
  3. You should not use document.onclick. Use non-intrusive document.addEventListener.
  4. You can't just force all <a> clicks to issue a visitWebPage, when the ones Munchkin supports are already bound to clickLink! This will create duplicates depending on network conditions, which will be very hard to troubleshoot. 

You need to identify your special links so only those links become custom visitWebPage calls.

 

Do this by decorating the interesting links with an HTML data- attribute. 

 

Then look for that attribute and only send the custom call if it exists. Note: if you have a link that would ordinarily be logged (as a clickLink) by Munchkin, and you want to control it with your custom code instead, add class="mchNoDecorate" to the link.

 

 

<a data-munchkin-log-as-visit href="#internal-navigation">Some app menu item</a> <script> document.addEventListener("click", function(event) { if( event.target.hasAttribute("data-munchkin-log-as-visit") ) { var urlThatWasClicked = 'https://console.someurl.com/dashboard'; Munchkin.munchkinFunction("visitWebPage", { url : urlThatWasClicked }); } }); </script>

 

 

1 reply

SanfordWhiteman
New Participant
April 16, 2020

Please don't post screenshots of code, post actual text highlighted using the syntax highlighter.

 

 

After you delete the image and replace it with text, we'll continue.

New Participant
April 16, 2020

I updated image with code snipped. Kindly share your inputs.

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
April 16, 2020

Thanks for making that change.

 

There are several problems with your code.

 

  1.  No need to insert your custom listener inside the standard Munchkin init routine. Keep it in a separate function or else it can easily be replaced by someone updating the embed code.
  2. You don't need the legacy event || window.event, I doubt your site actually supports IE 8.
  3. You should not use document.onclick. Use non-intrusive document.addEventListener.
  4. You can't just force all <a> clicks to issue a visitWebPage, when the ones Munchkin supports are already bound to clickLink! This will create duplicates depending on network conditions, which will be very hard to troubleshoot. 

You need to identify your special links so only those links become custom visitWebPage calls.

 

Do this by decorating the interesting links with an HTML data- attribute. 

 

Then look for that attribute and only send the custom call if it exists. Note: if you have a link that would ordinarily be logged (as a clickLink) by Munchkin, and you want to control it with your custom code instead, add class="mchNoDecorate" to the link.

 

 

<a data-munchkin-log-as-visit href="#internal-navigation">Some app menu item</a> <script> document.addEventListener("click", function(event) { if( event.target.hasAttribute("data-munchkin-log-as-visit") ) { var urlThatWasClicked = 'https://console.someurl.com/dashboard'; Munchkin.munchkinFunction("visitWebPage", { url : urlThatWasClicked }); } }); </script>