📜 ⬆️ ⬇️

Display advertisements Google AdSense, on ajax downloadable pages

Good afternoon. Yesterday I came across an unpleasant fact, namely, the AdSense code only works when the page loads. If you try to process the AdSense code that is in the data that we receive using ajax, then nothing will come of it (only an error will be issued). The downloadable Google script uses document.write, which only works when the page loads. But the page is already loaded. And I started digging ...

For a start, I found an interesting article about extsrc.js , a good, sensible article, a working code. But it is good if there is only one ad unit in the downloadable content. Then it will work without problems. I have such ad units - 2. Why this method will not work?

So, the AdSense code is placed like this:

One
')
<script type="text/javascript">
google_ad_client = "ca-pub-436345435345";
google_ad_slot = "33454355080";
google_ad_width = 728;
google_ad_height = 90;
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


Two

<script type="text/javascript">
google_ad_client = "ca-pub-436345435345";
google_ad_slot = "3345434255080";
google_ad_width = 728;
google_ad_height = 90;
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


The show_ads.js script intercepts the variables in front of it (google_ad_client, google_ad_slot, etc.), processes it, and resets them. And since the code is processed strictly in turn, show_ads.js has non-empty variables every time.

Using extsrc.js, show_ads.js runs after the page is drawn. And the first run of this script handles the last variables, because the names of the variables in the AdSense code are the same, and are assigned from top to bottom. The script worked, based on the data of the last block, and then everything was reset. Only the banner of the last block will appear, but in the place of the first one. Running the rest of the scripts is meaningless, there is no data for them.

Well, besides, when using extsrc.js, you need to change the google script download code.

It was: <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
So: <script type="text/javascript" extsrc="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
This is a violation of the rules, the code can not be changed.

But maybe I found a way out. Delving into the network, I came across one project - writeCapture . One of its features is to execute JS code in which document.write is present. Moreover, this code can be executed in the specified block. Using it is very simple:

$('#').writeCapture().html('<script src="http://.js"></script>');

At the <script src = “ Your Script.js ”> </ script> you can insert any js code.

So, I get the data on ajax. In this data, I have a block with an adsense code. Suppose this is a block id = "Reklama". The data we want to put somewhere on the page. I mostly work with jquery, but all sorts of html, append, etc. Well tolerate anything, but if the block has the code <script type = "text / javascript"> something </ script> and we put it using html, append, etc. then we will not see this code in the block later. I do not understand why this is happening, maybe in the comments who will explain? And if we saw it, this code would immediately work, and we would get an error about the impossibility of the document.write operation. I found a way out by implementing the resulting code with the construct

document.getElementById('X').innerHTML=" ajax"

So the code is transferred to where we want, without any changes, along with <script type = "text / javascript"> something </ script>, with this construction, the script body does not work.

Finally, the most interesting, do this:

$('#Reklama').writeCapture().html($('#Reklama').html());

Banner appeared. We look at the code, everything is as it should be, but there are not enough lines from the AdSense code itself before the banner. Of course, there is nothing criminal here, but we will make some changes for the sake of art. Nobody forbids using append instead of html.

$('#Reklama').writeCapture().append($('#Reklama').html());

Hooray! We see the banner! We look at the html code. AdSense code as live.

But it turned out that this is not all. The number of banner impressions, google enters into the global array of the page, i.e. a kind of counter. As soon as the counter reaches a number greater than 3, banners no longer appear. Google, the owner of his code and we did not touch him, since it is impossible. And we are masters of our code and browser. Before showing banners, I do this:

delete __google_ad_urls;
delete google_iframe_oncopy;
delete google_jobrunner;
delete google_jobrunner;
delete google_persistent_state;
delete google_persistent_state_async;



delete google_num_ad_slots;
delete google_num_0ad_slots;
delete google_prev_ad_slotnames_by_region;
delete google_unique_id;


I delete so many arrays, just because I haven’t completely disassembled the show_ads.js code, so that's for sure. Upon subsequent display of the banner, these arrays are successfully restored.

Although you can go through all the variables and delete those where the string "google" is found, but this method is good only for those who have ads on the page, and there are no others from Google.

Here, for example:
for (var key in window)
{if (key.toLowerCase (). indexOf ('google')> - 1 && key! = 'google_ad_client' && key! = 'google_ad_slot' && key! = 'google_ad_width' && key! = 'google_ad_height')
{window [key] = undefined;}}

Here, of course, indiscriminately, all objects are sorted out, but if there is nothing Google on the page, except for banners, then it will quite work for you. I note that I did not use delete window [key], since I’m below version 9, I don’t want to eat this.

Good luck, and constructive criticism to me.

Source: https://habr.com/ru/post/128858/


All Articles