Prepare to test campaigns (iOS)

In this phase, you will:

  • Implement all required API methods to support campaign testing
  • Create a hidden debug menu that enables internal testing and troubleshooting
  • Learn how to interpret Contextual Commerce logs

Set up required methods

Preparing your SDK for data validation includes setting up methods to track page views and orders.

To enable and test campaign functionality, you must implement the following additional methods:

Track product

Track the products shoppers view, and all their available variations, to ensure campaigns match them with the most relevant products and promotions.

Call the trackProduct method immediately after trackPageView on any product page load. For example:

  • On application launch to a product page
  • On application resume to a product page
  • On any in-app navigation that presents a product page

The method requires you to provide information about the products being viewed:

  • product: An array of all active SKUs on the page. Contextual Commerce uses this list as source data for the setCurrentProduct method. This enables the application to match shoppers with specific specials or hot items. Include every available product variant. 
  • currentProductCarterCallback: A closure invoked by an in-campaign "Add To Cart" button that simulates the shopper adding the currently selected SKU to their cart. This callback triggers the exact behavior of your native Add to Cart button. If the shopper has not selected required options (for example, size or color), this method initiates the standard application flow to collect that information before adding the item to the cart.

For more information about the trackProduct method, refer to the trackProduct method in the class reference articleOpens in new tab.

The following is an example of the trackProduct method:

Code
 try? Granify.trackProduct(
            products: [
                Product(
                    sku: "myprod_001",
                    productId: "myprod",
                    title: "My Product",
                    price: Price(amount: 100.0),
                    regularPrice: Price(amount: 100.01),
                    image: URL(string: "path/to/image"),
                    url: URL(string: "path/to/product")
                ),
                ... // more variations of this product
            ],
        ) {
            // your code for adding this product to the cart...
            self.addProductToCart(id: "myprod")
        }

Track cart state

Track products in the shopper's cart to enable cart-specific campaigns types, such as abandoned cart reminders.

The trackCart method should be called:

  • On application launch, even if the cart is empty
  • After any cart update
    • Item added, removed, or quantity changed
    • Purchase completed and cart cleared
    • Cart synced from another device

The method requires you to provide information about the products currently in the shopper's cart:

  • items: A dictionary of all items in the shopper's cart and their quantities. Pass the complete contents with every call, even if the cart is empty.

For more information about the trackCart method, refer to the trackCart method in the class reference articleOpens in new tab.

The following is an example of the  trackCart method:

Code
try? Granify.trackCart(items: [
  product1: 1,
  product2: 4,
  product3: 2
])

Set the currently viewed product 

Track the precise product variation the shopper has chosen to ensure campaigns make personalized product recommendations based on the exact SKU being viewed. This method functions as a lookup utility. It uses the provided strings to select a specific variation from the list previously provided in your trackProduct call. 

Call the setCurrentProduct method immediately after each trackProduct invocation and whenever the shopper changes the chosen item on a product page, for example:

  • On application launch to a product page 
    • Pass the default selected product SKU
  • On application resume on a product page
    • Pass the selected product SKU when the view loads
  • From navigation to a product page
    • Pass the default selected product SKU
  • On user changing the selected product variation like size or color
    • Pass the new selected product SKU

The method requires you to provide information about the product variation the shopper has selected:

  • sku: The SKU of the currently selected product variation. Must match one of the sku values in your trackProduct call.
  •  productId: The parent product ID. Must match the  productId from your trackProduct call.

For more information about the setCurrentProduct method, refer to the setCurrentProduct method in the class reference articleOpens in new tab.

The following is an example of the setCurrentProduct method:

Code
try? Granify.setCurrentProduct(sku: "myprod_001", productId: "myprod")

Show campaigns

Set up the showConcept method to enable internal campaign testing in your app. This method requests a specific campaign to display in your environment, allowing you and the Contextual Commerce QA team to preview and validate campaigns before they go live. Call the showConcept method any time after you call  activateGranify.

Note: The showConcept method is necessary for internal testing and campaign approval and should never be used during regular shopper sessions.

The method requires you to provide information about the products currently in the shopper's cart:

  • conceptId: A text string representing the campaign to test. Reach out to your Account Manager for your specific campaign ID. 

For more information about the showConcept method, refer to the showConcept method in the class reference articleOpens in new tab.

The following is an example of the showConcept method:

Code
try? Granify.showConcept(conceptId: "1234")

Create a debug menu

A hidden debug menu allows the Contextual Commerce QA team and your internal testers to trigger specific tests directly within your app without disrupting users or modifying production code. This menu is intended exclusively for internal testing and should be hidden from shoppers. 

Recommended implementation

Your debug menu should be:

  • Available within QA or Test builds of your app 
  • Accessible through a special gesture or interaction
    • Ex: triple-tap on a particular screen or hold down a button for five seconds before the "developer" UI appears
  • Clearly distinguished from any regular app operations

Your debug menu UI should include:

  • A text field for entering the specific concept ID associated with the test campaign
  • A way to manually trigger the showConcept method

Analyzing logs

The Contextual Commerce SDK generates logs at various levels (debug, info, call, warn, and error) during its operation. To simplify log analysis, each log entry begins with the prefix [Granify] making it easy to filter relevant entries. 

The following is an example of a log entry you might find:

Code
[Granify] 🟡 warn [consume(data:):65] Force Matching Failure: CampaignFilter::7641 failed has_cart: ["+"] for value: false

Interpreting this example:

  • Campaign ID 7641 was triggered using the showConcept method, but the SDK couldn't display it.
  • The campaign has an unmet requirement that the shopper must have items in their cart.
  • Specifically, has_cart: ["+"] means "requires at least one cart item," and value: false indicates the current shopper's cart was empty when the campaign was triggered.
Provide feedback button