Prev | Current Page 383 | Next

David Berube

"Practical Reporting with Ruby and Rails"

Such errors would only get worse as
the scale of the calculation increased.
Next, you iterate through the results, adding each one to an array until the required
number of clicks is reached:
@results = []
click_sum = 0
results_raw.each do |r|
@results << r
click_sum += r.clicks
break if click_sum > @target_clicks
end
@estimated_clicks = click_sum
@avg_cost_per_click = (
@results.inject(0.0) { |sum,r| sum+=r.cost } ) / (
@results.inject(0.0) { |sum,r| sum+= r.clicks } )
CHAPTER 13 n TRACKING YOUR ADS WITH GOOGLE ADWORDS 279
As you can see, the loop through results_raw is used to fill the @results array. It loops
through all of the results and adds them into the array. It also adds to the click_sum
counter, and when that??™s equal to the number of clicks you are looking for, you stop
adding values to the array. Since you sorted by the cost per click, you end up with an
array of the ads with the lowest cost per click that totals the amount of clicks you are
seeking.
The two @results.inject structures look complicated, but they simply sum the cost
and the clicks fields, respectively.


Pages:
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
Pozycjonowanie Wrocław nocleg lodz pompy ciepła kasta.brene.pl Wczasy nad morzem