Using Contribution History
These examples assume donors
Since this data uses historical contribution data, all of these code examples assume the user is a donor. You can ensure this either by segmenting to donors, or by adding conditional statements.
Supporter Records
One thing you can do for a user is list them back their contribution history for a supporter record email.
Your supporter record:
{% for contrib in contribution.contributions %}
{{ contrib.transaction_dt }}: ${{contrib.transaction_amt }}
{% endfor%}
And that will result in something like:
Your supporter record:
2013-01-16 12:22:33: $100.00
2012-12-12 11:34:11: $44.01
2012-11-14 01:32:12: $10000.00
If you didn't want to include the time and wanted a nicer more readable date format, didn't want to include cents, and wanted to add thousands-separators for large contributions:
{% for contrib in contribution.contributions %}
{{ contrib.transaction_dt | date("F j, Y") }}:
${{ contrib.transaction_amt | number_format }}
{% endfor%}
And that would result in:
January 16, 2013: $100
December 12, 2012: $44
November 14, 2012: $10,000
Advanced Contribution Asks
In addition to iterating over the data for display, you can use the values to derive advanced contribution metrics. This includes things such as:
- Average contribution.
- Total contributed
- Dates and amounts of the users oldest and newest recent contributions
- Number of contributions
- The above for contributions filtered by date, contribution page, or contribution type (online, offline, recurring, non-recurring).
Data columns
Because of the data format, sometimes you want to grab a column of data from the contribution data object.To assist with that, we've added a convience function, column()
, for fetching a column of similar values.
For example, you'll often want to grab all the transaction_amts
:
{{ column(contribution.contributions, 'transaction_amt') }}
If you want to get a list of all the contribution amounts in descending order, you'd do:
{{ column(contribution.contributions,'transaction_amt') | sort | reverse}}
Second Highest Previous Contribution
This code would ask for the second highest previous contribution as an ask, with a fallback both for donors with only one gift as well as non-donors, rounded and capped between $25 and $200:
{% set amts = column(contribution.contributions, 'transaction_amt')| sort | reverse %}
{% set min_ask = 25 %}
{% set max_ask = 200 %}
{% set ask = amts[1] ?: amts[0] | default(min_ask) %}
{% set ask = min(max(min_ask, ask), max_ask) | round %}
Will you donate ${{ ask }} today?
Total Contributed, Number of Contributions, and Average Contribution:
For the donation history from above, this would result in:
{% set amts = column(contribution.contributions, 'transaction_amt') %}
- Number of donations {{ amts | length }}
- Total donated ${{ sum(amts) }}
- Average donation ${{ sum(amts)/amts|length }}
- Rounded average donation: ${{ (sum(amts)/ amts | length) | round }}
Beware of calculating Average donation for non-donors
If you try to calculate Average Donation for a non-donor, you'll end up dividing by 0, which results in nothing being output, which is never what you want. The way to avoid this is to wrap the calculation in a conditional statement.
Oldest and Newest Contributions and Dates:
Date of first contribution: {{ contribution.contributions | sort_by("transaction_dt") | first.transaction_dt }}
Date of last contribution: {{ contribution.contributions | sort_by("transaction_dt") | last.transaction_dt }}
First donation amount:
{{ contribution.contributions | sort_by("transaction_dt") | first.transaction_amt }}
Last donation amount :
{{ contribution.contributions | sort_by("transaction_dt") | last.transaction_amt }}
Filtering
You can also filter the contributions used for the above types of operations, using the where
filter.
For example, the following would create a new contribution data object for all contributions made during 2014:
{% set contribs_2014 = contribution.contributions | where({"transaction_dt":{"lte": "2014-01-01 00:00:00", "gt": "2015-01-01 00:00:00"}}) %}
If you wanted to filter in a way that wasn't possible with the where()
filter, you could utilize a for-if
loop to build a new array.
For example, to exclude contributions to a specific set of pages:
{% set contrib_subset = [] %}
{% for contrib in contribution.contributions if contrib.contribution_page_id not in [13,14,15]%}
{% set contrib_subset = contrib_subset | merge([contrib]) %}
{% endfor %}
If only wanted to include donations after a specific date that were not recurring, you can combine equality comparisons with the inequality comparison object:
{% set contrib_subset = contribution.contributions | where({"is_recurring":0, "transaction_dt": {"gt":"2014-11-02 00:00:00"}}) %}
Updated less than a minute ago