Mixpanel Community Icon

how to sum the property value which is in array

·
·

how to sum the property value which is in array

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    Hi Poornima, Please stand by as I search the Mixpanel knowledge base for an AI-powered answer. I normally take about 15 seconds or so. In case AI cannot answer your question, feel free to reach out to Mixpanel support at mixpanel.com/get-support. If you have follow-up questions for this thread, please tag me again with your question: @kapa.ai <follow-up question> đź‘‹

    Feel free to ask a follow-up question once I've answered by mentioning me with @kapa.ai

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    To sum property values in an array in Mixpanel, you have several options depending on your specific use case:

    Using List Properties

    If you have a list property (array) in your events, Mixpanel provides built-in functionality to work with these values:

    1. For List of Objects

    If you have a list of objects property (like items in a cart), you can use the built-in "Sum" computation: 1. Select your list of objects property 2. Choose the "Sum" computation 3. Select the numeric property within the objects that you want to sumThis will calculate the total for the whole list for that property, as described in the List of Objects Property Support documentation.

    2. Using Custom Properties

    You can create a custom property using the sum() function to sum all numbers in a list: LBKSsum(list)LBKSFor example, if you have a list of prices called priceList = [5,205,178,12,22], you can use: LBKSsum(priceList)LBKSThis would return 422, as explained in the Custom Properties Functions documentation.

    3. Using JQL (JavaScript Query Language)

    For more advanced use cases, you can use JQL with the built-in reducers: LBKSfunction main() { return Events({ from_date: "2023-01-01", to_date: "2023-01-31", event_selectors: [{event: "Your Event"}] }) .map(function(event) { // Sum the values in your array property return event.properties.your_array.reduce(function(sum, value) { return sum + value; }, 0); }) .reduce(mixpanel.reducer.sum()); }LBKSAlternatively, you can use the mixpanel.reducer.object_merge() function to merge objects with numeric values, as shown in the JQL Builtins documentation.Remember that list properties in Mixpanel have certain limits: Event Property lists are limited to 8KB, and User Profile Property lists are limited to 256KB.

    kapa.ai is still learning and improving, please let me know how I did by reacting below.

    ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.