Expand my Community achievements bar.

Need to track Products added to the Cart in Mobile App

Avatar

Level 2

Hi,

 

We need to track the products that users add to their cart in our mobile app. Currently, we are fully dependent on the app developers. We will provide the code for tracking products in the cart, and they will integrate that code into the app. Additionally, we are not using 'Data collection'(Adobe Launch) at this time.

 

Can anyone please share the code details to track products on the Cart Page?

4 Replies

Avatar

Level 5

Hi @Sivateja_raju 

 

Would like to try to help but need to clarify the following - Are you using the latest AEP Mobile SDK? Native code or with framework like ReactNative? Using leagcy trackState/Action or sendEvent?

 

Thanks,

John

 

Avatar

Adobe Champion

Hey @Sivateja_raju ,

Ideally, the solution would be bespoke to your specific app and events but I can share some generic code reference that you can share with your developer.

 

For iOS Swift Application-

 

 

import AEPAnalytics

func trackCartAddEvent(productId: String, category: String, productName: String, quantity: String, price: String) {
    let products = "\(category);\(productName);\(quantity);\(price)"
    
    let contextData: [String: Any] = [
        "&&products": products,
        "cartItemId": productId
    ]
    
    // Send the data to Adobe Analytics
    Analytics.trackAction("scAdd", data: contextData)
}

 

 

For Android-

 

import com.adobe.marketing.mobile.Analytics;
import java.util.HashMap;
import java.util.Map;

public void trackCartAddEvent(String productId, String category, String productName, String quantity, String price) {
    String products = category + ";" + productName + ";" + quantity + ";" + price;
    
    Map<String, Object> contextData = new HashMap<>();
    contextData.put("&&products", products);
    contextData.put("cartItemId", productId); // Example of a custom variable for the cart item ID

    // Send the data to Adobe Analytics
    Analytics.trackAction("scAdd", contextData);
}

 

 In here -

 

  • products follows the format category;productName;quantity;price. You can get the reference from s.products documentation here.
  • scAdd represents the standard cart addition event in Adobe Analytics. You can add more events here accordingly such as scCheckout, purchase etc.
  • contextData is used to pass additional information (e.g., cartItemId). You would use this to map values in Adobe Analytics variables through processing rules. Look here for more info.

    Hope this helps!