Implement inline campaigns (Android Views)

This article describes how to implement Contextual Commerce inline campaigns in your Android application.

Step 1: Add the GranifyInlineView to the layout

Adding a GranifyInlineView  requires the following steps: 
  1. Open your layout file. To support inline campaigns, you will need to add a GranifyInlineView to every position on a page where you want an inline to appear. 
  2. The GranifyInlineView class extends FrameLayout and can be added to your user interface in the same way.
  3. Set the layout_height to 0 so it is not initially visible.

XML example (recommended constraints): 

XML
<com.granifyinc.granifysdk.campaigns.inlineView.GranifyInlineView
  android:id="@+id/granifyInlineViewBelowProductImage"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  app:layout_constraintBottom_toTopOf="@+id/colour_spinner"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toBottomOf="@id/productPageImageView" />


Android ListView layouts are not supported for inlines due to the remeasuring of the ListView and its items on every animation frame. RecyclerView can be used as an alternative. 

Step 2: Set expand and collapse handlers

Contextual Commerce requires clients to pass in two handlers in the onCreate() method of the current activity:
  1. An expand handler of type GranifyInlineViewExpander that will expand the view, allowing an inline campaign to appear.
  2. A collapse handler of type GranifyInlineViewCollapser that will collapse the view back to its initial state (height = 0px).
  3. Pass these handlers through GranifyInlineView.setExpandHandler and GranifyInlineView.setCollapseHandler.

Kotlin example: 

Code
inlineView.setExpandHandler { granifyInlineView, height ->
  val params = granifyInlineView.layoutParams
  granifyInlineView.layoutParams = params.apply { this.height = height }
  }
  inlineView.setCollapseHandler { granifyInlineView ->
  val params = granifyInlineView.layoutParams
  granifyInlineView.layoutParams = params.apply { this.height = height }
  }


The SDK handles the animation logic internally. Do not add any custom animation handling to the view or its parent layout. 

Step 3: Configure the label

GranifyInlineViewLabel identifies unique locations on a page and must not be duplicated within the same page. 

Product pages:

  1. Set the productId field. This is required when the GranifyInlineView is located on a product page. 
  2. Set the sku field (optional). This is required when the view is associated with a specific productID/sku combination. 

Example:

Code
GranifyInlineView(name="aboveProductPhoto", productId="Pants", sku="Green") 

Cart pages:

  1. Set the productId for any inline tied to a specific item. 
  2. Use only the name field for areas not tied to a specific product (for example at the top of the page). 

Step 4: Finalize integration

Follow these steps to complete the implementation process:
  1. Compile a list of all GranifyInlineViewLabel locations you have added to the application. 
  2. Send this list to your Contextual Commerce account manager to create inline campaigns.
Do not change these label names after this point without notifying your Contextual Commerce account manager, as doing so will break live campaigns.  
Provide feedback button