GuidesAPI ReferenceChangelog
GuidesAPI ReferenceChangelogLog In

Java API Client Library

 

Introduction

The Java Criteo API Library provides a set of classes and functions that allow for quick programmatic access to your Criteo data and Ad Set controls. This Transition SDK will help you migrate from MAPI to Criteo API. We will support it until end of 2021 when we will decommission the /legacy endpoints. After that date we will only support our official versioned Client Libraries available in Q3 2021.

Building the API client library requires:

  • Criteo account
  • Java 1.8+
  • Maven/Gradle

Installation

To install the API client library to your local Maven repository, simply execute:

mvn clean install

or

gradle install

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

mvn clean deploy

Refer to the OSSRH Guide for more information.

Maven users

Add this dependency to your project's POM:

<dependency>
  <groupId>com.criteo</groupId>
  <artifactId>marketing.java-client</artifactId>
  <version>1.0.28</version>
  <scope>compile</scope>
</dependency>

Gradle users

Add this dependency to your project's build file:

compile "com.criteo:marketing.java-client:1.0.28"

Others

Clone GitHub repository and generate the JAR by executing:

>>git clone https://github.com/criteo/criteo-java-marketing-transition-sdk.git 
>>cd criteo-java-marketing-transition-sdk
>>mvn clean package

Compile the your Java program with classpath that includes JARs:

  • target/marketing.java-client-1.0.28.jar
  • target/lib/*.jar

Here is an example:

>> cd criteo-java-marketing-transition-sdk/src/examples/java

>>javac -classpath "/Users/<USERDIR>/criteo-java-marketing-transition-sdk/target/api-transition.java-client-2021-04.0.jar:/Users/<USERDIR>/criteo-java-marketing-transition-sdk/target/lib/*:./*" com/criteo/marketing/examples/StatisticsApplication.java
>>java -Xdiag -classpath "/Users/<USERDIR>/criteo-java-marketing-transition-sdk/target/api-transition.java-client-2021-04.0.jar:/Users/<USERDIR>/criteo-java-marketing-transition-sdk/target/lib/*:./*" com.criteo.marketing.examples.StatisticsApplication

Make sure that the Java class is compiled at the same version of Java as the version with which you try to run it. For example, if you compiled your class with Java 11 then it should be run with the same version, otherwise you will get java.lang.UnsupportedClassVersionError.

Example of usage with OAuth

This example demonstrates retrieving statistics data using token-based OAuth authentication. As a reminder, access tokens are credentials used to access protected resources. An access token is a string representing an authorization issued to the client. The string is usually opaque to the client. Tokens represent specific scopes and durations of access, granted by the resource owner (in this case same as client), and enforced by the resource server and authorization server.

package com.criteo.marketing.examples;

import com.criteo.marketing.ApiClient;
import com.criteo.marketing.ApiException;
import com.criteo.marketing.ApiResponse;
import com.criteo.marketing.Configuration;
import com.criteo.marketing.api.AnalyticsApi;
import com.criteo.marketing.model.StatisticsReportQueryMessage;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.List;

public class StatisticsApplication {
    public static void main(String[] args) {
        ApiClient client = Configuration.getDefaultApiClient();
        client.setUsername("client_key");
        client.setPassword("client_secret");

        // Uncomment the following line to enable debugging logs.
        client.setDebugging(true);

        AnalyticsApi myApi = new AnalyticsApi(client);
        List<StatisticsReportQueryMessage.DimensionsEnum> dimensions = Arrays.asList(StatisticsReportQueryMessage.DimensionsEnum.ADSET);
        List<String> metrics = Arrays.asList("Clicks");
        // despite using OffsetDateTime type the Analytics endpoint supports only the day part; i.e data will be returned for the day specified.
        OffsetDateTime startDate = OffsetDateTime.parse("2020-12-03T00:00:00+01:00");
        OffsetDateTime endDate = OffsetDateTime.parse("2020-12-04T23:59:59+01:00");

        StatisticsReportQueryMessage statisticsReportQueryMessage = new StatisticsReportQueryMessage();
        //if statisticsReportQueryMessage.advertiserIds is omitted the data will be fetched for the entire portfolio
        statisticsReportQueryMessage.advertiserIds("1234");
        statisticsReportQueryMessage.dimensions(dimensions);
        statisticsReportQueryMessage.metrics(metrics);
        statisticsReportQueryMessage.startDate(startDate);
        statisticsReportQueryMessage.endDate(endDate);
        statisticsReportQueryMessage.currency("EUR");
        statisticsReportQueryMessage.format("Csv");
        try {
            ApiResponse<byte[]> response = myApi.getAdsetReportWithHttpInfo (statisticsReportQueryMessage);
        } catch (ApiException e) {
            System.out.println(e.getCode());
            System.out.println(e.getResponseBody());
        }
    }
}

 

This guide provides a quick start with Criteo Java SDK Library and its usage with Analytics endpoint. The guide will be updated with more use cases and examples at a later date.