PDA

View Full Version : Randomly running some code


Jon12345
10-24-2013, 11:12 AM
I have some ad code, but only want to show it with a 1 in 10 chance. The code is:

<div data-type="ad" data-publisher="abc.mysite.site" data-zone="ron" data-format="728x90"></div>

It will be in my postbit legacy template.

Is there any vbulletin code that allows me to do this?

Thanks,

Jon

kh99
10-24-2013, 12:36 PM
I think this would work:
<if condition="TIMENOW%10==0">
<div data-type="ad" data-publisher="abc.mysite.site" data-zone="ron" data-format="728x90"></div>
</if>


Basically, whenever the current time (in seconds) ends in 0 it will show.

Jon12345
10-24-2013, 12:42 PM
Clever idea, thanks! I will try that. :)

--------------- Added 1382625240 at 1382625240 ---------------

Seems to work. :) Would be nice to work out how to vary the probability of display. e.g 1 in 7 or whatever. So, I could say decide to set it to 1 in 7 or 1 in 5 etc.

squidsk
10-24-2013, 03:15 PM
Clever idea, thanks! I will try that. :)

--------------- Added 1382625240 at 1382625240 ---------------

Seems to work. :) Would be nice to work out how to vary the probability of display. e.g 1 in 7 or whatever. So, I could say decide to set it to 1 in 7 or 1 in 5 etc.
Switch the number (i.e. 10) after the % to whatever you want, so 1 in 7 change the 10 to a 7, 1 in 5 change the 10 to a 5.

kh99
10-24-2013, 06:08 PM
Yeah, what squidsk said. I simplified the explanation a little, the code actually divides the unix time (seconds since Jan 1 1980 or something like that) by ten and checks that the remainder is 0, which happens when the last digit is 0 (since we're dividing by 10). But you can just as easily divide by 7 (or whatever) and check for a remainder of 0 to get a 1 in 7 chance.

Jon12345
10-25-2013, 12:31 PM
I've tried using 2 and it showed my ads 1 in 2 chance. But when I tried 4 or 7, it didn't show 1 in 4 or 1 in 7 times. Maybe the maths is a bit out somehow? Do I need to clip off the decimals or something?

kh99
10-25-2013, 12:42 PM
Hmm...'%' should divide integers and return the remainder, so I don't think decimals should matter. So you're saying condition="TIMENOW%7==0" doesn't work right?

Jon12345
10-25-2013, 01:02 PM
I am trying 1 in 4 right now. TIMENOW%4==0

See it in action (or not as the case may be!):http://access-programmers.co.uk/forums/showthread.php?p=1301767&mode=linear#post1301767

The third post has a 300x250 advert. There should be another one appearing to the right of that one 1 in 4 times. But it doesn't.

kh99
10-25-2013, 01:22 PM
OK, I ran a test and it seems to work. Are you saying that the exact code works with 10 or 2 but not 4 or 7 (I guess I'm saying, are you sure there's not a typo)? If you do this (add the 1|| in the condition):
<if condtion="1||TIMENOW%4==0">
Ad code
</if>


then it should show every time and you can make sure there's not something else wrong.

The only other thing I can think of is that it's not exactly random because it's based on the time, so if for example you have it set to 4 and you constantly hit reload, and the page happens to take about 4 seconds to load each time, you may end up with something that looks like it's not a 1 in 4 chance.

Jon12345
10-25-2013, 02:16 PM
My code is:

<if condition="TIMENOW%4==0">
<td>
<div data-type="ad" data-publisher="xxx.mysite.site" data-zone="ron" data-format="300x250"></div>
</td>
</if>

I got it to show 2 out of 13 goes. Maybe I was just unlucky with my first few tries. I will try 1 in 3. :)

kh99
10-25-2013, 02:24 PM
Well, if I view the html source of the page you linked to above, and search for 'data-zone="ron" data-format="300x250"', I find it once, but occasionally I'll find it twice. So it kind of seems to me like it might be working, but I don't see any ads at all on that page.

Edit: Also (possibly related), when I look at the JS console I see this:

[11:25:24.433] ReferenceError: GS_googleAddAdSenseService is not defined @ http://access-programmers.co.uk/forums/showthread.php?p=1301767&mode=linear#post1301767:59
[11:25:24.433] ReferenceError: GA_googleAddSlot is not defined @ http://access-programmers.co.uk/forums/showthread.php?p=1301767&mode=linear#post1301767:63
[11:25:24.433] ReferenceError: GA_googleFetchAds is not defined @ http://access-programmers.co.uk/forums/showthread.php?p=1301767&mode=linear#post1301767:72
[11:25:24.565] ReferenceError: GA_googleFillSlot is not defined @ http://access-programmers.co.uk/forums/showthread.php?p=1301767&mode=linear#post1301767:569
[11:25:24.572] ReferenceError: GA_googleFillSlot is not defined @ http://access-programmers.co.uk/forums/showthread.php?p=1301767&mode=linear#post1301767:906
[11:25:25.095] The Web Console logging API (console.log, console.info, console.warn, console.error) has been disabled by a script on this page.
[11:25:25.240] Use of attributes' nodeValue attribute is deprecated. Use value instead. @ http://cdn1.developermedia.com/a.min.js:1

Jon12345
10-25-2013, 02:28 PM
Something doesn't seem quite right. For 1 in 3, I am now at 9 out of 13.

kh99
10-25-2013, 03:09 PM
OK, well maybe there's something about the way it works that I don't understand. Anyway, here's another one you can try:
<if condition="(((float)$GLOBALS[pagestarttime])*1000)%4==0">


That one doesn't work too well for me because strangely some of the digits of what's supposed to be the current time with microseconds don't change from one page to another. But apparently it's OS dependent and I'm running on my home Windows 7 computer, so it might work better for you.

Edit:...otherwise, it's not too difficult to write a plugin that does an actual random number.

Jon12345
10-29-2013, 05:41 PM
How do I reverse the condition?

I have an advert showing if condition=x. But I want to stop showing an advert in another location is condition=x. So, what is the reverse?

Edit:Worked it out. Use != instead of ==