Expand my Community achievements bar.

Join us January 15th for an AMA with Champion Achaia Walton, who will be talking about her article on Event-Based Reporting and Measuring Content Groups!
SOLVED

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?

1 Accepted Solution

Avatar

Correct answer by
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!

 

View solution in original post

11 Replies

Avatar

Level 6

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

Correct answer by
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!

 

Avatar

Level 2

Hi @igupta,

We have implemented below code in our App

val add-OnsDataMap = mutableMapOf<String, String>()
add-OnsDataMap["&&events"] = "scCheckout"
// Product = 1662. Quantity = 1, Revenue = 49:99 //
add-OnsDataMap["&&products"] = ";1662;1;49.99";

Analytics.trackState["scCheckout", add-OnsDataMap];

But, Only scCheckout details are captured in Analytics. Quantity and Revenue details are not captured in Analytics.
Can you please help me to resolve this issue?

Sivateja_raju_0-1734440712145.png







 

Avatar

Adobe Champion

Hey @Sivateja_raju ,

Have you implemented this for purchase event? Also, have you mapped contextData elements with respective variables through processing rules?

 

Best,

Isha

Avatar

Level 2

@igupta,

I have implemented only scCheckout event and I am not using any Context data Variables and Values.

Avatar

Adobe Champion

hey, so in your screenshot I can see there is a value for units and revenue. Can you sort by each and check what they are attributed to?

 

Best,

Isha

Avatar

Level 2

@igupta 

I have used Purchase Event while testing. That's why Units and Revenue values are captured.

Sivateja_raju_1-1734449623468.png

 

 

Avatar

Level 2

@igupta 

Purchase Event code:

val add-OnsDataMap = mutableMapOf<String, String>()
add-OnsDataMap["&&events"] = "purchase"
add-OnsDataMap["&&products"] = ";Unlimited Data;1;49.99";

add-OnsDataMap["orderid"] = "4567123890";