dp6 Marketing Attribution Models

repository·master·Indexed 18 days ago

https://github.com/dp6/marketing-attribution-models

A Python library for solving digital marketing attribution problems. It implements heuristic models (Last Interaction, First Interaction, Linear, Time Decay, Position Based, and Last Click Non-Direct) and algorithmic models (Shapley Value and Markov Chains) to credit conversions across multi-channel customer journeys. The library includes the MAM class for data processing, journey ID creation, and visualization of results.

Tokens
4.2K
Snippets
14
Records
16
Agent score
13%

What's inside marketing-attribution-models

  1. Understand the available attribution models

    master

    The library supports two main categories of attribution models to credit conversions across multi-channel customer journeys:

    Heuristic Models

    These are rule-based models that assign credit based on specific touchpoint positions or patterns:

    • Last Interaction: Credits 100% of the conversion to the very last touchpoint.
    • Last Click Non-Direct: Ignores direct traffic and credits 100% to the last non-direct channel.
    • First Interaction: Credits 100% of the conversion to the first touchpoint.
    • Linear: Distributes credit equally across all touchpoints in the journey.
    • Time Decay: Assigns more credit to touchpoints that occurred closer to the time of conversion.
    • Position Based: Assigns 40% to the first touchpoint, 40% to the last, and distributes the remaining 20% equally among middle touchpoints.

    Algorithmic Models

    These use mathematical frameworks to estimate channel contribution:

    • Shapley Value: Based on Game Theory. It estimates a channel's contribution by calculating the marginal contribution of that channel across all possible permutations of the journey. Note that complexity increases exponentially ($2^n$) with the number of channels.
    • Markov Chains: Uses a stochastic process and a Transition Matrix to model the probability of moving between channels. It calculates credit using the Removal Effect: the ratio of the difference between the general conversion probability and the conversion probability when a specific channel is removed.
  2. Initialize the MAM Object with different data templates

    master

    The MAM class can be initialized using two different input Data Frame templates, determined by the group_channels parameter:

    • group_channels=True: The input DataFrame contains one session per row. Each row must include a unique user identifier, a boolean column for conversion, and the session's source channel.
    • group_channels=False: The input DataFrame contains the entire journey per row. Channels and time-to-conversion are aggregated into strings separated by a delimiter (default is '>'), which can be customized using path_separator.

    To generate a dummy dataset for testing, set random_df=True.

    # Scenario: group_channels = True (one session per row)
    attributions = MAM(df,
        group_channels=True,
        channels_colname='channels',
        journey_with_conv_colname='has_transaction',
        group_channels_by_id_list=['user_id'],
        group_timestamp_colname='visitStartTime',
        create_journey_id_based_on_conversion=True)
    
    # Scenario: Quick testing with random data
    attributions = MAM(random_df=True)
  3. Optimize Markov models for large datasets using conversion_value_as_frequency

    master

    When working with extremely large datasets, running the Markov model can cause memory or processing issues. You can mitigate this by using a grouped dataset based on the journey path and the conversion_value_as_frequency parameter.

    Workflow

    1. Transform your data: Instead of using raw journey rows, group your data by the journey path and conversion status, then count the occurrences. Example SQL transformation:
      SELECT
        journey,
        has_conversion,
        COUNT(*) AS occurrences
      FROM original_database
      GROUP BY journey, has_conversion
    2. Configure the MAM object: When instantiating the MAM object, set the conversion_value parameter to the name of your new frequency/count column (e.g., 'occurrences').
    3. Execute the model: When calling .attribution_markov(), you must set conversion_value_as_frequency=True to ensure the model processes the grouped data correctly.

    This approach drastically reduces the database size while preserving information, yielding the same results as the standard method but with significantly lower memory overhead.

    # 1. Instantiate MAM with the frequency column assigned to 'conversion_value'
    mam_grouped = MAM(df=grouped_df,
                group_channels=False,
                conversion_value='occurrences',
                channels_colname='journey',
                journey_with_conv_colname='has_conversion',
                time_till_conv_colname='skip_column')
    
    # 2. Call attribution_markov with conversion_value_as_frequency=True
    results_markov_grouped = mam_grouped.attribution_markov(transition_to_same_state=True,
                                                conversion_value_as_frequency=True)
  4. Configure journey ID creation in MAM

    master

    If your input data does not have a unique journey ID, you can use the create_journey_id_based_on_conversion parameter. When set to True, the class generates a new ID based on:

    1. The columns specified in group_channels_by_id_list (e.g., ['user_id']).
    2. The conversion column specified in journey_with_conv_colname.

    This process orders sessions for each user and creates a new journey ID for every conversion event. Note that for business-specific logic (like breaking journeys after a period of inactivity), manual customization is recommended.

  5. Analyze attribution results by channel

    master

    To view attribution results aggregated by channel rather than by individual journey, use the .group_by_channels_models attribute. This attribute is updated automatically whenever an attribution model is called. It allows for easy comparison between different models (e.g., comparing a heuristic model against an algorithmic one).

    # View results grouped by channel
    attributions.group_by_channels_models
  6. Initialize the MAM object

    master

    To use the MAM class, you must first prepare a pandas DataFrame containing session-level data. Because raw data is often at the session granularity, you must configure the MAM object to group sessions into journeys using unique identifiers (like a visitor ID) and timestamps.

    Key parameters for initialization:

    • channels_colname: The column containing the marketing channel names.
    • group_channels: Set to True to aggregate sessions into journeys.
    • group_channels_by_id_list: A list of columns used to identify a unique user/entity (e.g., ['fullVisitorId']).
    • group_timestamp_colname: The column used to order the journey.
    • journey_with_conv_colname: A boolean column indicating if a conversion occurred in that session.
    • create_journey_id_based_on_conversion: Set to True if you need the library to generate journey IDs based on conversion events.
    • conversion_value: The column representing the value of the conversion (e.g., revenue).
    from marketing_attribution_models import MAM
    
    DP_tribution = MAM(
        df, 
        channels_colname='channelGrouping', 
        group_channels=True, 
        group_channels_by_id_list=['fullVisitorId'], 
        group_timestamp_colname='date_tratada', 
        journey_with_conv_colname='has_transaction', 
        create_journey_id_based_on_conversion=True, 
        conversion_value='totals_transactionRevenue'
    )
  7. Compare different attribution models

    master

    After running multiple models (e.g., Shapley and Markov), you can compare their results using the .group_by_channels_models attribute. This attribute returns a collection of all models applied to the MAM instance.

    You can also run specific heuristic models like Last Click using .attribution_last_click() and visualize results using the .plot() method.

    # Compare all applied models
    comparison = DP_tribution.group_by_channels_models
    print(comparison.sum())
    
    # Run a specific heuristic model
    last_click = DP_tribution.attribution_last_click()
    
    # Plot results
    DP_tribution.plot(model_type='heuristic')
  8. Visualize attribution results

    master

    You can plot and compare results from different models using the .plot() method.

    • To plot all stored models: attributions.plot()
    • To plot only algorithmic models: attributions.plot(model_type='algorithmic')
    # Plot all models
    attributions.plot()
    
    # Plot only algorithmic models
    attributions.plot(model_type='algorithmic')
  9. Run the Shapley Value attribution model

    master

    The .attribution_shapley() method calculates channel contributions using the Shapley Value methodology.

    Parameters:

    • size: Limits the number of unique channels in a journey. The default is the 4 last channels. This is used because the number of iterations grows exponentially ($2^N$) with the number of channels.
    • order: Determines how marginal contributions are calculated. By default, it calculates the contribution of the channel combination independent of the order in which they appear in journeys.
    • values_col: Specifies what the Shapley Value is calculated against. The default is the conversion rate (which accounts for non-conversions). You can also specify columns for total conversions or total conversion value.

    Returns: A tuple containing:

    1. Results grouped by unique journeys.
    2. Results grouped by channels.
    shapley_results = DP_tribution.attribution_shapley()
    
    # Accessing results
    journey_results = shapley_results[0]
    channel_results = shapley_results[1].reset_index()
  10. Access processed data via the .DataFrame attribute

    master

    The .DataFrame attribute provides access to the underlying database after it has been processed by the MAM object. It includes added columns such as journey_id and aggregated columns like channels_agg, time_till_conv_agg, converted_agg, and conversion_value.

    This attribute is updated every time an attribution model is run, but modifying it directly will not affect the results of subsequent model calculations.

    # View the processed database
    attributions.DataFrame