> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kleep.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Mobiili-SDK - Android

**Tietovarasto:** [https://github.com/KlipFit/kleep-android](https://github.com/KlipFit/kleep-android)

## Asennus

***

Tätä prosessia tarvitsee noudattaa vain kerran, ensimmäisellä kerralla kun asennat SDK:n projektiisi. Katso myöhempiä päivityksiä varten ohjeet **Päivitys**-osiosta.

1. Lisää *gradle.properties*-tiedostoosi tämä token:

   `authToken=jp_9ibcequhrjtge3cmavt2k4sq6a`

2. Lisää *settings.gradle*- tai *settings.gradle.kts*-tiedostoosi seuraavat rivit:

```groovy theme={null}
// for *settings.gradle*

dependencyResolutionManagement {
*// ...*
    repositories {
        *// ...*
~~~~        maven {
            url "https://jitpack.io"
            credentials { username authToken }
        }
    }
}
```

```kotlin theme={null}
// for *settings.gradle.kts*

*dependencyResolutionManagement {
// ...
    repositories {
	      // ... 
        maven {
            url = uri("https://jitpack.io")
            credentials(PasswordCredentials::class) {
                val authToken: String by settings
                username = authToken*
            }
        *}
    }
}*
```

1. Lisää moduulisi *build.gradle*- tai *build.gradle.kts*-tiedostoon seuraavat rivit riippuvuuden luomiseksi:

```groovy theme={null}
// *build.gradle*
dependencies {
	implementation 'com.github.KlipFit:kleep-android:version'
}
```

```kotlin theme={null}
// *build.gradle.kts*
dependencies {
	implementation("com.github.KlipFit:kleep-android:version")
}
```

## **Alustus**

***

Muokkaa omaa `Application`-luokkaasi ja anna `applicationContext` alustusta varten.

```kotlin theme={null}
import com.kleep.sdk.KleepResult
import com.kleep.sdk.Kleep

class CustomApplication: Application() {

    override fun onCreate() {
        super.onCreate()
        Kleep.init(applicationContext)
    }
}
```

Muista lisätä Application-luokkasi **`Manifest.xml`**-tiedostoon

```kotlin theme={null}
<application
        android:name=".CustomApplication"
        ...
        >
...
</application>
```

## Tuonti

***

Voit tuoda moduulin manuaalisesti **`MainActivity.kt`**-tiedostosi alussa

```kotlin theme={null}
import com.kleep.sdk.KleepResult
import com.kleep.sdk.Kleep
```

## Käyttö

***

SDK:ssa on 3 metodia:

| metodi                            | latausaika                    |
| --------------------------------- | ----------------------------- |
| `show`                            | Ensimmäinen käynnistys: 250ms |
| Seuraavat käynnistykset: 50–250ms |                               |
| `requestSize`                     | 250ms                         |
| `track`                           | 250ms                         |

### Metodi 1: `requestSize`

Tämä metodi mahdollistaa CTA:n logiikan toteuttamisen.

**Logiikkakaavio**

Tämä kaavio selittää, kuinka päivitetään Kleepin avaava CTA.

[https://www.figma.com/board/BlurZ01lR3JBQZeTUU98TE/Mobile---CTA-Logic?node-id=0-1\&t=ccXWciNziIdfhgew-1](https://www.figma.com/board/BlurZ01lR3JBQZeTUU98TE/Mobile---CTA-Logic?node-id=0-1\&t=ccXWciNziIdfhgew-1)

**Toteutusesimerkki**

```kotlin theme={null}
Kleep.builder(this, onResult = {
	when (it) {
	    is KleepResult.NotSupported -> {
	        // Handle the NotSupported case
	    }
	
	    is KleepResult.NoRecommendationYet -> {
	        // Handle the NoRecommendationYet case
	    }
	
	    is KleepResult.RecommendationFound -> {
	        val resultText = it.size
	        val variantId = it.variantId
	        Toast.makeText(this, resultText, Toast.LENGTH_SHORT).show()         
      }

      else -> {

      }
    }
})
	.setPackageName(packageName)         // optional
	.setLang(DEFAULT_LANGUAGE)           // required: "fr", "en", "es", "it", "pt", "nl"
	.setProductId("1234")                // required
	.setRetailer("retailer_name")        // required
	.setTrackingId("uuid")               // empty if non-existent 
	.setCustomerId("unique_customer_id") // optional
	.requestSize()
```

```kotlin theme={null}
package com.kleep.sdk

/**
 * Interface with the Kleep SDK in order to compute the recommended size of a product given some user information
 */
interface KleepDialogBuilder {
    /**
     * Set the package name, not required
     * @return interface of the SDK builder
     */
    fun setPackageName(packageName: String): KleepDialogBuilder
    
    /**
     * Set the language to use: required (default to FR)
     * Values supported: "fr", "en", "es", "it", "pt", "nl"
     * @return interface of the SDK builder
     */
    fun setLang(lang: String): KleepDialogBuilder

    /**
     * Set product ID, required to compute the recommended size.
     * @return interface of the SDK builder
     */
    fun setProductId(productId: String): KleepDialogBuilder

    /**
     * Set tracking id, required. Should represent unique tracking id.
     * Should be an empty string if the tracking id does not exist.
     *
     * @return interface of the SDK builder
     */
    fun setTrackingId(tracking_id: String): KleepDialogBuilder

    /**
     * Set customer id, optonal. Should represent customer id linked to backend.
     * Should be an empty string if the tracking id does not exist.
     *
     * @return interface of the SDK builder
     */
    fun setCustomerId(customer_id: String): KleepDialogBuilder
    
    /**
     * Set retail name (mandatory), required for stock/similar_products requests.
     *
     * @return interface of the SDK builder
     */
    fun setRetailer(retailerName: String): KleepDialogBuilder
    
    /**
     * Sets the stock availability for items.
     * Providing an empty map or omitting this function disables stock checking.
     *
     * @return interface of the SDK builder
     */
    fun setStocks(stocks: Map<String, Boolean>): KleepDialogBuilder

    /**
     * Display the Kleep UI inside a BottomSheet, so the user can fill a survey or take 2 pictures
     * in order to compute and display the recommended size to the user
     */
    fun show()

    /**
     * Get the latest recommended size for the last viewed user.
     * As it's an async process and take several seconds,
		 * the result will be give by the onResult listener , provided to the SDK builder
     */
    fun requestSize()
}
```

### Metodi 2: `show`

Tätä metodia kutsutaan, kun CTA:ta klikataan. Se mahdollistaa eri näyttöjen lataamisen. **SDK:n integroivan kehittäjän tulee upottaa ne bottom sheet -komponenttiin.**

| parametri                                                                                                  | prioriteetti  | kuvaus                                                      |
| ---------------------------------------------------------------------------------------------------------- | ------------- | ----------------------------------------------------------- |
| **productID**                                                                                              | *pakollinen*  | Tuotteesi ID                                                |
| **retailer**                                                                                               | *pakollinen*  | Jälleenmyyjän nimi                                          |
| **customerID**                                                                                             | *valinnainen* | CRM-tunniste, käytetään analytiikkaan                       |
| **trackingID**                                                                                             | *valinnainen* | Ulkoisen seurantatarjoajan käyttämä ID                      |
| stocks                                                                                                     | *valinnainen* | Kartta tuotevarianttien ID:istä niiden varastotilanteeseen. |
| `true` varastossa, `false` (oletuksena) loppu varastosta, emptyMap tai ei määritelty ohittaa tarkistuksen. |               |                                                             |

```kotlin theme={null}
Kleep.builder(this, onResult = {
	when (it) {
	    is KleepResult.NotSupported -> {
	        // Handle the NotSupported case
	    }
	
	    is KleepResult.NoRecommendationYet -> {
	        // Handle the NoRecommendationYet casem
	    }
	
	    is KleepResult.RecommendationFound -> {
	        val resultText = it.size
	        val variantId = it.variantId
	        // addToCart(variantId)
	        
	        Toast.makeText(this, resultText, Toast.LENGTH_SHORT).show()
      }

      else -> {

      }
    }
})
	.setPackageName(packageName)         // optional
	.setLang(DEFAULT_LANGUAGE)           // required: "fr", "en", "es", "it", "pt", "nl"
	.setProductId("1234")                // required
	.setRetailer("retailer_name")        // required
	.setTrackingId("tracking_id")        // optional
	.setCustomerId("unique_customer_id") // optional
	.setStocks(stocks: Map<String, Boolean>) // optional
	.show()
```

### Metodi 3: `track`

Tämä metodi mahdollistaa omien tapahtumien seuraamisen.

```kotlin theme={null}
Kleep.track(eventName: String, eventInfo: Map<String, String>)
```

Haluamme seurata 3 tapahtumaa:

| eventName               | Laukaisija                                       |
| ----------------------- | ------------------------------------------------ |
| `product_viewed`        | PDP:n katselun yhteydessä                        |
| `product_added_to_cart` | Tuotteen lisäämisen yhteydessä ostoskoriin       |
| `checkout_completed`    | Tilauksen vahvistuksen yhteydessä maksun jälkeen |

**`product_viewed`**

[Parametrit (CSV)](/images/android/Untitled%202c68bfd3e84981db82a9f924cf43673d.csv)

**Esimerkki**

```jsx theme={null}
{
		productId: str
}
```

**`product_added_to_cart`**

[Parametrit (CSV)](/images/android/Untitled%202c68bfd3e8498100baf9d17e8b7df5e0.csv)

**Esimerkki**

```jsx theme={null}
{
    productId: str,
    variantId: str,
    cart: [
	    {
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
				sku: "XYZ",
		    size: "S",
		    quantity: 2,
		    price: {
						amount: "50",
				    currencyCode: "EUR"		    
		    }
	    },
	    {
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
		    sku: "XYZ",
		    size: "S",
		    quantity: 1,
		    price: {
				    amount: "40",
				    currencyCode: "USD"
				}
	    }
	    ...
    ],
}
```

**`checkout_completed`**

[Parametrit (CSV)](/images/android/Untitled%202c68bfd3e84981b0bdecd417ba621e4e.csv)

**Esimerkki**

```jsx theme={null}
{
    orderId: "000001",
    cart: [
	    {
		    lineItemId: "000001#1",
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
				sku: "XYZ",
		    size: "S",
		    quantity: 2,
		    price: {
						amount: "50",
				    currencyCode: "EUR"		    
		    }
	    },
	    {
			  lineItemId: "000000#2",
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
		    sku: "XYZ",
		    size: "S",
		    quantity: 1,
		    price: {
				    amount: "40",
				    currencyCode: "USD"
				}
	    }
	    ...
    ],
}
```
