Index: civicrm/trunk/sites/all/modules/wmf_owa/owa.module |
— | — | @@ -1,6 +1,33 @@ |
2 | 2 | <?php |
3 | 3 | |
| 4 | +/** The default OWA log script location **/ |
| 5 | +define( 'OWA_LOG_DEFAULT', 'http://analytics.tesla.usability.wikimedia.org/wiki/d/extensions/owa/log.php' ); |
| 6 | + |
4 | 7 | /** |
| 8 | + * Implementation of hook_perm(). |
| 9 | + */ |
| 10 | +function wmf_owa_perm() { |
| 11 | + return array('administer wmf_owa'); |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Implementation of hook_menu() |
| 16 | + */ |
| 17 | +function wmf_owa_menu() { |
| 18 | + $items = array(); |
| 19 | + |
| 20 | + $items['admin/settings/wmf_owa'] = array( |
| 21 | + 'title' => 'Wikimedia Foundation OWA Configuration', |
| 22 | + 'description' => t('Configure OWA log script location.'), |
| 23 | + 'access arguments' => array('administer wmf_owa'), |
| 24 | + 'page callback' => 'drupal_get_form', |
| 25 | + 'page arguments' => array( 'wmf_owa_settings' ), |
| 26 | + ); |
| 27 | + |
| 28 | + return $items; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
5 | 32 | * Callback for menu path "admin/settings/wmf_owa". |
6 | 33 | */ |
7 | 34 | function wmf_owa_settings() { |
— | — | @@ -8,40 +35,12 @@ |
9 | 36 | '#type' => 'textfield', |
10 | 37 | '#title' => t( 'OWA log path' ), |
11 | 38 | '#required' => TRUE, |
12 | | - '#default_value' => variable_get('wmf_owa_log', 'http://analytics.tesla.usability.wikimedia.org/wiki/d/extensions/owa/log.php'), |
| 39 | + '#default_value' => variable_get('wmf_owa_log', OWA_LOG_DEFAULT ), |
13 | 40 | ); |
| 41 | + return system_settings_form( $form ); |
14 | 42 | } |
15 | 43 | |
16 | 44 | /** |
17 | | - * Format and send contribution tracking data to OWA |
18 | | - * @param array $msg Array containing contribution information |
19 | | - */ |
20 | | -function wmf_owa_record_contribution( $msg ){ |
21 | | -//NEED: |
22 | | -//utm_campaign |
23 | | -//referer URL |
24 | | -//owa_session_id |
25 | | -//OWA log URL |
26 | | - |
27 | | - // format the contribution tracking message |
28 | | - $query_params = array( |
29 | | - "owa_event_type" => "ecommerce.transaction", |
30 | | - "owa_ct_order_id" => $msg[ 'contribution_tracking_id' ], |
31 | | - "owa_ct_order_source" => $data['utm_campaign'], |
32 | | - "owa_ct_total" => $msg['gross'], |
33 | | - "owa_ct_tax" => 0, |
34 | | - "owa_ct_shipping" => 0, |
35 | | - "owa_ct_gateway" => $msg['gateway'], |
36 | | - "owa_page_url" => $encodedReqURL, |
37 | | - //ct_line_items[li_product_name]=foo& |
38 | | - "owa_session_id"=> "some_id" |
39 | | - ); |
40 | | - |
41 | | - // send the tracking message to OWA |
42 | | - wmf_owa_send_tracking( $query_params ); |
43 | | -} |
44 | | - |
45 | | -/** |
46 | 45 | * Implementation of hook_civicrm_post |
47 | 46 | * |
48 | 47 | * @param unknown_type $op |
— | — | @@ -54,22 +53,59 @@ |
55 | 54 | if ( $objectName != 'Contribution' && !in_array( $op, array( 'edit', 'create' )) ) { |
56 | 55 | return; |
57 | 56 | } |
58 | | - var_dump( $objectRef ); exit; |
59 | | - // load the OWA |
60 | | - //$contribution_tracking_data = wmf_owa_get_contribution_tracking_data( $objectId ); |
| 57 | + |
| 58 | + // load the OWA-specific tracking data |
| 59 | + $contrib_tracking_data = wmf_owa_get_contribution_tracking_data( $objectId ); |
| 60 | + if ( !$contrib_tracking_data ) { |
| 61 | + // log, don't post to OWA |
| 62 | + watchdog( 'wmf_owa', 'Could not load tracking data for contribution_id ' . $objectId, WATCHDOG_WARNING ); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * pull the gateway out fo the trxn_id string |
| 68 | + * it is the element prior to the first space in the str |
| 69 | + */ |
| 70 | + $gateway = substr( $objectRef->trxn_id, 0, strpos( $objectRef->trxn_id, "\s" )); |
| 71 | + |
| 72 | + // format the contribution tracking message |
| 73 | + $query_params = array( |
| 74 | + "owa_event_type" => "ecommerce.transaction", |
| 75 | + "owa_ct_order_id" => $contrib_tracking_data->contribution_tracking_id, |
| 76 | + "owa_ct_order_source" => $contrib_tracking_data->utm_campaign, |
| 77 | + "owa_ct_total" => $objectRef->total_amount, |
| 78 | + "owa_ct_tax" => 0, |
| 79 | + "owa_ct_shipping" => 0, |
| 80 | + "owa_ct_gateway" => $gateway, |
| 81 | + "owa_page_url" => $contrib_tracking_data->url, |
| 82 | + "owa_session_id"=> $contrib_tracking_data->owa_session, |
| 83 | + ); |
| 84 | + |
| 85 | + // log the query params |
| 86 | + watchdog( 'wmf_owa', 'OWA tracking params for contribution_id ' . $objectId . ': ' . print_r( $query_params, true ), WATCHDOG_DEBUG ); |
| 87 | + |
| 88 | + // sent the query to OWA |
| 89 | + wmf_owa_send_tracking( $query_params ); |
| 90 | + return; |
61 | 91 | |
62 | 92 | } |
63 | 93 | |
64 | | - |
65 | 94 | /** |
66 | 95 | * Fetch info needed for OWA from contribution_tracking table |
67 | 96 | * @param int $contribution_tracking_id |
| 97 | + * @return array |
68 | 98 | */ |
69 | 99 | function wmf_owa_get_contribution_tracking_data( $contribution_id ) { |
70 | 100 | $query = 'SELECT ct.id as "contribuion_tracking_id", ct.owa_session, ctor.url, ct.utm_campaign |
71 | 101 | FROM contribution_tracking ct, contribution_tracking_owa_ref ctor |
72 | 102 | WHERE ctor.id=ct.owa_ref && ct.contribution_id="%d"'; |
73 | | - return db_fetch_object( db_query( $query, $contribution_id )); |
| 103 | + $result = db_fetch_object( db_query( $query, $contribution_id )); |
| 104 | + |
| 105 | + // log $query and $result |
| 106 | + watchdog( 'wmf_owa', 'OWA query to contribution_tracking for contribution_id ' . $contribution_id . ': ' . $query, WATCHDOG_DEBUG ); |
| 107 | + watchdog( 'wmf_owa', 'OWA query result from contribution_tracking for contribution_id ' . $contribution_id . ': ' . print_r( $result, true ), WATCHDOG_DEBUG ); |
| 108 | + |
| 109 | + return $result; |
74 | 110 | } |
75 | 111 | |
76 | 112 | /** |
— | — | @@ -78,10 +114,16 @@ |
79 | 115 | * @param array $query_params The array of data for OWA to track |
80 | 116 | */ |
81 | 117 | function wmf_owa_send_tracking( $query_params ) { |
82 | | - $owa_log_url = variable_get( 'wmf_owa_log', 'http://analytics.tesla.usability.wikimedia.org/wiki/d/extensions/owa/log.php' ); |
| 118 | + $owa_log_url = variable_get( 'wmf_owa_log', OWA_LOG_DEFAULT ); |
83 | 119 | $ch = curl_init(); |
84 | 120 | curl_setopt( $ch, CURLOPT_URL, $owa_log_url . "?". http_build_query( $query_params )); |
85 | 121 | curl_setopt( $ch, CURLOPT_USERAGENT, "WMF Ecommerce web service 1.0" ); |
86 | | - curl_exec( $ch ); |
| 122 | + |
| 123 | + if ( !curl_exec( $ch )) { |
| 124 | + // log curl error curl_error( $ch ); |
| 125 | + watchdog( 'wmf_owa', 'Error posting to OWA with query params: ' . print_r( $query_params, true ), WATCHDOG_ERROR ); |
| 126 | + } |
| 127 | + |
87 | 128 | curl_close( $ch ); |
| 129 | + return; |
88 | 130 | } |
\ No newline at end of file |