Home / Admin / om external links monsterlink
Duplicate Snippet

Embed Snippet on Your Site

om external links monsterlink

Code Preview
js
(function(window, document) {
	// Your campaign's UNIQUE ID
	var campaignSlug = 'YOUR-UNIQUE-ID';
	// Helper to get an anchor element.
	var getLink = function(element) {
		var parent = element;
		while (parent && 'A' !== parent.tagName) {
			parent = parent.parentNode;
		}
		return parent && 'A' === parent.tagName ? parent : null;
	}
	document.documentElement.addEventListener('click', function(evt) {
		evt = evt || window.event;
		var el = getLink(evt.target || evt.srcElement);
		if (!el) {
			// Not an anchor link
			return;
		}
		// Internal link, so Nothing to do here.
		if (el.hostname === location.hostname) {
			return;
		}
		var url = el.href;
		// if the link is ALREADY a MonsterLink, then original MonsterLink logic will be used.
		if ( (' ' + el.className + ' ').indexOf(' manual-optin-trigger ') > -1 || url.indexOf('https://app.monstercampaigns.com/c/') > -1) {
			return;
		}
		// Check if user has already completed this campaign.
		if ( document.cookie.indexOf('omSuccess-' + campaignSlug +'=') > -1) {
			return;
		}
		evt.preventDefault();
		window['om' + campaignSlug].startShow();
		
		// If the MonsterLink is CLOSED, then redirect to original href location of the link that is clicked.
		document.addEventListener('om.Campaign.cleanup', function(evt) {
			if ( evt.detail.Campaign.id === campaignSlug ) {
				setTimeout( function() {
					window.location.href = url;
				}, 200 );
			}
		});
	});
})(window, document);

Comments

Add a Comment