Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions dynamicprice/util/src/jvmMain/kotlin/AdManager.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
package adsbynimbus.solutions.dynamicprice.util

import com.google.api.ads.admanager.axis.factory.*
import com.google.api.ads.admanager.axis.v202408.LineItemCreativeAssociationService
import com.google.api.ads.admanager.axis.v202505.*
import com.google.api.ads.admanager.axis.v202508.*
import com.google.api.ads.admanager.lib.client.*
import com.google.api.ads.common.lib.auth.*
import com.google.api.client.auth.oauth2.*
import kotlinx.coroutines.*

actual suspend fun main(args: Array<String>) {
val context: AdManagerAxisClient = adManagerContext(
Expand Down
111 changes: 111 additions & 0 deletions dynamicprice/util/src/jvmMain/kotlin/AdaptiveBanner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package adsbynimbus.solutions.dynamicprice.util

import com.google.api.ads.admanager.axis.v202508.*
import kotlinx.coroutines.delay

inline val Size.isBanner get() = width == 320 && height == 50
inline val Size.isInterstitial get() = width == 320 && height == 480

suspend fun AdManagerAxisClient.addAdaptiveBannerSupport(
orders: Collection<Long>,
sizeId: Long,
bannerCreative: Long,
mrecCreative: Long,
interstitialCreative: Long,
bannerTargetingName: String = "Nimbus Banner",
interstitialTargetingName: String = "Nimbus Interstitial",

) {
var totalLines = 0
for (orderId in orders) {
val orderLines = statement {
where("orderId = :orderId")
withBindVariableValue("orderId", orderId)
}
do {
lineItemService.getLineItemsByStatement(orderLines.toStatement()).run {
totalLines = totalResultSetSize
val updates = results?.onEach { line ->
line.creativeTargetings = arrayOf(
CreativeTargeting().also {
it.name = bannerTargetingName
it.targeting = Targeting().apply {
customTargeting = CustomCriteriaSet().apply {
logicalOperator = CustomCriteriaSetLogicalOperator.OR
children = arrayOf(
CustomCriteria().apply {
keyId = sizeId
valueIds = longArrayOf(mrecCreative, interstitialCreative)
operator = CustomCriteriaComparisonOperator.IS_NOT
},
)
}
}
},
CreativeTargeting().also {
it.name = interstitialTargetingName
it.targeting = Targeting().apply {
customTargeting = CustomCriteriaSet().apply {
logicalOperator = CustomCriteriaSetLogicalOperator.OR
children = arrayOf(
CustomCriteria().apply {
keyId = sizeId
valueIds = longArrayOf(bannerCreative, mrecCreative)
operator = CustomCriteriaComparisonOperator.IS_NOT
},
)
}
}
}
)
line.creativePlaceholders.onEach {
when {
it.size.isBanner -> it.targetingName = bannerTargetingName
it.size.isInterstitial -> it.targetingName = interstitialTargetingName
}
}
}
delay(1000)
lineItemService.updateLineItems(updates)
delay(1000)
orderLines.increaseOffsetBy(pageSize)
}
} while (orderLines.offset < totalLines)
totalLines = 0
}

val bannerCreative = statement {
where("creativeId = :creativeId")
withBindVariableValue("creativeId", bannerCreative)
}
val interstitialCreative = statement {
where("creativeId = :creativeId")
withBindVariableValue("creativeId", interstitialCreative)
}
totalLines = 0
do {
lineItemCreativeService.getLineItemCreativeAssociationsByStatement(bannerCreative.toStatement()).run {
totalLines = totalResultSetSize
val updates = results?.onEach {
it.targetingName = bannerTargetingName
}
delay(1000)
lineItemCreativeService.updateLineItemCreativeAssociations(updates)
delay(1000)
bannerCreative.increaseOffsetBy(pageSize)
}
} while (bannerCreative.offset < totalLines)
totalLines = 0
do {
lineItemCreativeService.getLineItemCreativeAssociationsByStatement(interstitialCreative.toStatement()).run {
totalLines = totalResultSetSize
val updates = results.onEach {
it.apply { targetingName = interstitialTargetingName }
}
delay(1000)
lineItemCreativeService.updateLineItemCreativeAssociations(updates)
delay(1000)
interstitialCreative.increaseOffsetBy(pageSize)
}
} while (interstitialCreative.offset < totalLines)
}
11 changes: 7 additions & 4 deletions dynamicprice/util/src/jvmMain/kotlin/DynamicPrice.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package adsbynimbus.solutions.dynamicprice.util

import com.google.api.ads.admanager.axis.v202505.*
import com.google.api.ads.admanager.axis.v202505.CustomTargetingKeyType.*
import com.google.api.ads.admanager.axis.v202508.*
import com.google.api.ads.admanager.axis.v202508.CustomTargetingKeyType.*
import java.text.DecimalFormat
import kotlinx.coroutines.delay
import kotlin.collections.addAll
Expand Down Expand Up @@ -217,7 +217,7 @@ suspend fun AdManagerAxisClient.findOrCreateCreatives(
""".trimIndent()
}
}
if (newCreatives.size > 0) {
if (newCreatives.isNotEmpty()) {
addAll(creativeService.createCreatives(newCreatives.toTypedArray()))
}
}
Expand Down Expand Up @@ -327,5 +327,8 @@ suspend fun AdManagerAxisClient.associateCreatives(
}
}
}
lineItemCreativeService.createLineItemCreativeAssociations(associations.toTypedArray())
associations.windowed(size = pageSize, step = pageSize, partialWindows = true).forEach {
lineItemCreativeService.createLineItemCreativeAssociations(it.toTypedArray())
delay(1000)
}
}
6 changes: 3 additions & 3 deletions dynamicprice/util/src/jvmMain/kotlin/Queries.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package adsbynimbus.solutions.dynamicprice.util

import com.google.api.ads.admanager.axis.utils.v202505.*
import com.google.api.ads.admanager.axis.v202505.*
import com.google.api.ads.admanager.axis.utils.v202508.*
import com.google.api.ads.admanager.axis.v202508.*

fun findBy(id: Long): Statement = statement {
where("id = :id")
Expand All @@ -10,7 +10,7 @@ fun findBy(id: Long): Statement = statement {

fun findBy(name: String): Statement = statement {
where("name = :name")
withBindVariableValue("name", "$name")
withBindVariableValue("name", name)
}.toStatement()

fun findAllBy(name: String): Statement = statement {
Expand Down
41 changes: 41 additions & 0 deletions dynamicprice/util/src/jvmMain/kotlin/Targeting.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package adsbynimbus.solutions.dynamicprice.util

import com.google.api.ads.admanager.axis.v202508.*
import kotlinx.coroutines.delay

suspend fun AdManagerAxisClient.addTargeting(
orders: Collection<Long>,
key: Long,
values: LongArray,
) {
var totalLines = 0
for (orderId in orders) {
val orderLines = statement {
where("orderId = :orderId")
withBindVariableValue("orderId", orderId)
}
do {
lineItemService.getLineItemsByStatement(orderLines.toStatement()).run {
totalLines = totalResultSetSize
val updates = results?.onEach { line ->
line.targeting.customTargeting?.apply {
children.filterIsInstance<CustomCriteriaSet>().onEach {
if (it.logicalOperator == CustomCriteriaSetLogicalOperator.AND) {
it.children += CustomCriteria().apply {
keyId = key
valueIds = values
operator = CustomCriteriaComparisonOperator.IS_NOT
}
}
}
}
}
delay(1000)
lineItemService.updateLineItems(updates)
delay(1000)
orderLines.increaseOffsetBy(pageSize)
}
} while (orderLines.offset < totalLines)
totalLines = 0
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ androidx-lifecycle = "2.9.2"
androidx-navigation = "2.9.3"
androidx-startup = "1.2.0"

api-admanager = "5.9.0"
api-admanager = "5.10.0"

compose = "1.9.0"
compose-activity = "1.10.1"
Expand Down
Loading