Using JS
This forum comment and this gist demonstrate a way to use a URL parameter to populate a field which is in the current page. This is an approach which works for inputs on the page (eg a text input for donation amount), even hidden ones. It's a very quick demo but might deliver what you need ...
As part of a custom extension
One of the common needs of a contribution or event registration system is to track what campaign that transaction is destined from and what link originated the transaction. This allows organizations to calculate what links / web sites / advertising / mailings are most effective and use that information to influence their current or future campaigns. Some organizations (e.g. wikimedia foundation) use a three level tracking scheme: Campaign, Appeal and Fund, while other organizations might choose to use only one tracking field.
This issue came up at the San Francisco developer camp. There is no built-in support for this in CiviContribute or CiviEvent. However we can accomplish something very similar using a combination of custom fields, populating fields via a GET url and a civicrm buildForm hook. We've made some changes to v2.2.4 to make it easier. Here is the recipe for tracking either a contribution or event registration page.
- Create a custom groupcalled "Contribution Tracker" which extends Contribution. Within that group create three custom fields, one each for "Campaign", "Appeal" and "Fund". In my test setup I created these as searchable text fields with a length of 64 characters. Lets assume these custom fields have ids of 4, 5 and 6.
For Drupal: Administer -> Customize -> Custom Data ->> New Group of Custom Fields
- Add these three fields to a Profilewhich is included on that contribution page. let assume the contribution page has an id of 1. As of v2.2.5 release, these profile fields will be hidden via javascript.
For Drupal: Administer -> Customize -> Profile ->> New CiviCRM Profile
In a Drupal module (lets call it civitracker) create the following hook:
- Make sure your civitracker module is enabled in Drupal.
- In the links that you add to your blog post or website, should pass in the values of the above three custom fields as GET parameters. Thus a sample URL would look like:
http://drupal.demo.civicrm.org/civicrm/contribute/transact?reset=1&id=1&custom_4=Campaign2009&custom_5=Summer&custom_6=General
 | CiviReport ships with a few reports that will analyze contributions based on "Campaign", "Appeal" and "Fund" custom data fields (these are being built for the Wikimedia Foundation). You can read more detailed specifications of the reports on the wiki. Some of the above changes are in v2.2.4. |
 | The issues that were filed (and fixed) to get the above working include: |