Skip to content

How to use componentity ads

Mr. Code (@mymakarim) edited this page Dec 15, 2021 · 13 revisions

Usage

1. Paste this script in the footer

<script type="text/javascript">
 
// 	adsense @mymakarim start
	console.log("START AD: ----------------");

const websitename = 'paikaftab.com';
function createAdImage(src, href) {
	let anchor = document.createElement('a');
	anchor.href = href + '?source='+websitename;
	anchor.target="_blank";
    let el = document.createElement('img');
    el.src = src;
	anchor.appendChild(el)
    return anchor;
}
			function createAdSlot(id) {
	let div = document.createElement('div');
	div.id = id
	div.style="max-width:350px; margin: 0 auto;"
	return div;
}
	
	function insertAsNumber( element, parent, id){
		if(parent){
  if ( parent.children.length > 2 ){
	  console.log("PARENT CHILDREN > 2: ELEMENT: ", element)
   parent.insertBefore(element, parent.children[id]);
 }
 else parent.appendChild(element);
			}
		console.log("STILL LOGGING")
}
	
var articleSelector = 'div.single_content';
	insertAsNumber( createAdSlot('componentity-ads-innerone'), document.querySelector(articleSelector), 6);
	insertAsNumber( createAdSlot('componentity-ads-innertwo'), document.querySelector(articleSelector), 12);

    fetch('https://componentity-ads.herokuapp.com/ads?websites.name='+websitename).then(function (response) {
	// The API call was successful!
	if (response.ok) {
		return response.json();
	} else {
		return Promise.reject(response);
	}
}).then(function (data) {
// 	This is the JSON from our response
	console.log("MAP PLACES FIND THE NAME OF WEBSITE THAT MATCHES AND PRINTOUT THE SLOTS");
		
				
		data.map((item)=>{
			let imagesrc = item.Image.url;
			let ahref = item.href;
		
			item.places.map((place)=>{
		var GivenDate = place.to;
		var CurrentDate = new Date();
		GivenDate = new Date(GivenDate);

		if(GivenDate >= CurrentDate && place.website.name===websitename){
// 			console.log("IT IS NOT EXPIRED & IS KHABARNAMA");
// 			console.log("MAP SLOTS and put them in adslots")
			place.slots.map((slot, index)=>{
				console.log("SLOT NAME: ", "componentity-ads-"+slot.name)
		let adslot = document.querySelector('#componentity-ads-'+slot.name);
				if(adslot){
					if(slot.name !== 'banner' && adslot.childElementCount === 0){
					adslot.innerHTML= `<div style="display: flex; justify-content: space-between;  align-items: center; margin-bottom: 10px; margin-top: 10px;">
	<h6 style="font-weight: semibold; line-height:1; margin: 0;">
		تبلیغ
	</h6>
	<a style="font-size: 12px; text-decoration:underline;" href="https://api.whatsapp.com/send?phone=93749996550&text=salam,lets talk about ads in paikaftab" target="_blank">برای نشر تبلیغ تان اینجا کلیک کنید!</a>
</div>`;}
				adslot.appendChild(createAdImage(imagesrc, ahref));	
				}
			})
	}else{
// 							console.log("EXPIRED OR IS NOT PAIKAFTAB.COM");
							console.log("WEBSITE NAME", place.website.name)
// 			console.log("WEBSITENAME: ", websitename)
							}})
		})
}).catch(function (err) {
	// There was an error
	console.warn('Something went wrong.', err);
});
	
		console.log("END AD: ----------------");

function createAdSlot(id) {
	let div = document.createElement('div');
	div.id = id
	return div;
}
	
	function insertAsNumber( element, parent, id){
  if ( parent.children.length > 2 ){
	  console.log("PARENT CHILDREN > 2: ELEMENT: ", element)
   parent.insertBefore(element, parent.children[id]);
 }
 else parent.appendChild(element);
		console.log("STILL LOGGING")
}
	
var articleSelector = '#articleid';
	insertAsNumber( createAdSlot('componentity-ads-innerone'), document.querySelector(articleSelector), 5);
	insertAsNumber( createAdSlot('componentity-ads-innertwo'), document.querySelector(articleSelector), 8);



// 	adsense @mymakarim end
</script>

2. Specify your slots with 'componentity-ads-' prefix as following

<div id="componentity-ads-SLUGOFYOURSLOT"></div>