All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gravity.gdk.impression.Impression.scala Maven / Gradle / Ivy

The newest version!
package com.gravity.gdk.impression

import com.gravity.gdk.config.DefaultSettings
import com.gravity.gdk.placement.PlacementKey
import com.gravity.gdk.reco.{RecoArticle, RecoArticleInImpression, RecoContext, RecoResult}
import play.api.libs.json.Json

import scala.concurrent._
import scalaj.http.{BaseHttp, Http}

/*
              ___...---''
  ___...---'\'___
''       _.-''  _`'.______\\.
        /_.)  )..-  __..--'\\
       (    __..--''
        '-''\@


 Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ
*/

object Impression {
  /**
    * Logs an impression. By default, impression logging is done automatically when using
    * [[com.gravity.gdk.placement.Placement.getRecos]]. You only need to manually log an impression using this method
    * if you are passing FALSE for the logImpression param to [[com.gravity.gdk.placement.Placement.getRecos]].
    *
    * @param recoResultF The future returned by [[com.gravity.gdk.placement.Placement.getRecos]].
    * @param articlesF If provided, should resolve with the articles to log in the impression. Normally you would
    *                  provide ALL the articles from recoResult; if that is the case you can omit this param and the
    *                  articles in the recoResult param will be used. However, in instances where you wish to, say,
    *                  rearrange or omit articles from recoResult, then you will want to indicate which articles you
    *                  are actually considering for the impression by passing them in this param.
    *
    * @return HTTP status code of the impression log result.
    */
  def log(
    placementKey: PlacementKey,
    recoResultF: Future[RecoResult],
    articlesF: Option[Future[List[RecoArticle]]] = None
  )(implicit recoCtx: RecoContext, http: BaseHttp = Http, ec: ExecutionContext): Future[Int] = {
    for {
      recoResult <- recoResultF
      articles <- articlesF.getOrElse(Future { recoResult.articles })
    } yield {
      val params = Json.obj(
        "currentUrl" -> recoCtx.currentUrl,
        "clientTimeMillis" -> System.currentTimeMillis().toString,
        "renderType" -> "api",
        "sitePlacementId" -> placementKey.placementId.toString,

        // We accept that this is typically unavailable in S2S scenarios and would require significant effort on our
        // clients to implement tracking of referrer URLs
        "referrer" -> "",

        "encodedImpressionSlug" -> recoResult.impressionSlug,
        "articles" -> RecoArticleInImpression.fromArticleList(articles)
      )

      val response = http(DefaultSettings.apiImpressionUrl)
        .postData(Json.stringify(params))
        .header("Content-Type", "application/json")
        .header("Accept", "application/json; version=0")
        .asString

      response.code
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy