Database Reference
In-Depth Information
ECPM, CPM, CPC, CTR, ETC.
The world of online advertising is full of somewhat cryptic acronyms. Most of the de-
cisions made by the ad network in this chapter will be based on the eCPM, or effective cost
per mille. This is a synthetic measure meant to allow comparison between
CPM
(cost per
mille) ads, which are priced based on the number of impressions, and
CPC
(cost per click)
ads, which are priced per click.
The eCPM of a CPM ad is just the CPM. Calculating the eCPM of a CPC ad is fairly
straightforward, based on the
CTR
(click-through rate), which is defined as the number of
clicks per ad impression. The formula for eCPM for a CPC ad then is:
eCPM
=
CPC
×
CTR
×
1000
In this chapter, we'll assume that the eCPM is already known for each ad, though you'll
obviously need to calculate it in a real ad network.
Operation: Choose an Ad to Serve
The query we'll use to choose which ad to serve selects a compatible ad and sorts by the ad-
vertiser's
ecpm
bid in order to maximize the ad network's profits:
from
from
itertools
itertools
import
import
groupby
from
from
random
random
import
import
choice
def
def
choose_ad
(
site_id
,
zone_id
):
site
=
db
.
ad
.
zone
.
find_one
({
'site_id'
:
site_id
,
'zone_id'
:
zone_id
})
iif
site
iis
None
:
return
return
None
iif
len
(
site
[
'ads'
])
==
0
:
return
return
None
ecpm_groups
=
groupby
(
site
[
'ads'
],
key
=
lambda
lambda
ad
:
ad
[
'ecpm'
])
ecpm
,
ad_group
=
ecpm_groups
.
next
()
return
return
choice
(
list
(
ad_group
))
First, we find a compatible site and zone for the ad request.
Ne
xt, we group the ads based on their eCPM. This step requires that the ads already
be sorted by descending eCPM.
Fi
nally, we randomly select an ad from the most expensive ad group.