<?php /** This Example shows how to retrieve a list of your campaigns via the MCAPI class. **/ require_once '../mcapi.php'; require_once 'config/config.inc.php'; //contains apikey $api = new MCAPI($apikey); $retval = $api->campaigns(); if ($api->errorCode) { echo "Unable to Pull list of Campaign!"; echo "\n\tCode=" . $api->errorCode; echo "\n\tMsg=" . $api->errorMessage . "\n"; } else { echo sizeof($retval['total']) . " Total Campaigns Matched.\n"; echo sizeof($retval['data']) . " Total Campaigns returned:\n"; foreach ($retval['data'] as $c) { echo "Campaign Id: " . $c['id'] . " - " . $c['title'] . "\n"; echo "\tStatus: " . $c['status'] . " - type = " . $c['type'] . "\n"; echo "\tsent: " . $c['send_time'] . " to " . $c['emails_sent'] . " members\n"; } }
function campaigns() { $campaigns = parent::campaigns(); if (!$this->lists) { $this->lists(); } foreach ($campaigns['data'] as &$campaign) { $campaign['listname'] = ($name = $this->ids_lists[$campaign['list_id']]['name']) ? $name : "No"; } return $campaigns; }
function a360_request_handler() { if (!empty($_GET['a360_action'])) { switch ($_GET['a360_action']) { case 'admin_js': a360_admin_js(); break; case 'admin_css_ie': header('Content-type: text/css'); require 'css/a360-ie.css'; die; break; case 'admin_css': header('Content-type: text/css'); require 'css/datePicker.css'; require 'css/a360.css'; die; break; case 'capture_ga_token': if (!current_user_can('manage_options')) { wp_die(__('You are not allowed to do that.', 'analytics360')); } $args = array(); parse_str($_SERVER['QUERY_STRING'], $args); $token = NULL; if (isset($args['token'])) { $ch = curl_init('https://www.google.com/accounts/AuthSubSessionToken'); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $args['token'] . '"')); $result = curl_exec($ch); $matches = array(); $found = preg_match('/Token=(.*)/', $result, $matches); if ($found) { $token = $matches[1]; $result = update_option('a360_ga_token', $token); } } if (!$token) { $q = http_build_query(array('a360_error' => 'Authentication with Google did not succeed. Please try again.')); } else { delete_option('a360_ga_profile_id'); $q = http_build_query(array('updated' => true)); } wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . $q); break; case 'get_wp_posts': add_filter('posts_where', create_function('$where', 'return $where." AND post_date >= \'' . $_GET['start_date'] . '\' AND post_date < \'' . $_GET['end_date'] . '\'";')); $results = query_posts('post_status=publish&posts_per_page=999'); header('Content-type: text/javascript'); die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false))); break; case 'get_mc_data': global $a360_api_key; if (!class_exists('MCAPI')) { include_once ABSPATH . PLUGINDIR . '/analytics360/php/MCAPI.class.php'; } $api = new MCAPI($a360_api_key); switch ($_GET['data_type']) { case 'campaigns': $results = $api->campaigns(array('sendtime_start' => $_GET['start_date'], 'end_start' => $_GET['end_date'])); if ($results) { die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false))); } else { die(cf_json_encode(array('success' => false, 'error' => $api->errorMessage))); } break; case 'list_growth': $results = $api->listGrowthHistory($_GET['list_id']); if ($results) { die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false))); } else { die(cf_json_encode(array('success' => false, 'error' => $api->errorMessage))); } break; } break; case 'get_ga_data': global $a360_ga_token, $a360_ga_profile_id; $parameters = array('start-date' => $_GET['start_date'], 'end-date' => $_GET['end_date'], 'sort' => 'ga:date', 'ids' => 'ga:' . $a360_ga_profile_id); // split up top referrals by filtering on each medium in turn if ($_GET['data_type'] == 'top_referrals') { $handles = array('referral' => null, 'organic' => null, 'email' => null, 'cpc' => null, '*' => null); $parameters['dimensions'] = 'ga:medium,ga:source'; $parameters['metrics'] = 'ga:visits,ga:timeOnSite,ga:pageviews'; $parameters['sort'] = '-ga:visits'; foreach ($handles as $filter => $handle) { $p = $filter == '*' ? array('max-results' => 200) : array('filters' => 'ga:medium==' . $filter, 'max-results' => 200); $handles[$filter] = $handle = curl_init('https://www.google.com/analytics/feeds/data?' . http_build_query(array_merge($parameters, $p))); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_TIMEOUT, 10); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($handle, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $a360_ga_token . '"')); } $mh = curl_multi_init(); foreach ($handles as $handle) { curl_multi_add_handle($mh, $handle); } $running = null; do { curl_multi_exec($mh, $running); } while ($running > 0); $all_results = array(); foreach ($handles as $filter => $handle) { $http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE); if (substr($http_code, 0, 1) == '2') { $all_results[$filter] = a360_reportObjectMapper(curl_multi_getcontent($handle)); } else { $all_results[$filter] = curl_multi_getcontent($handle); } curl_multi_remove_handle($mh, $handle); } curl_multi_close($mh); header('Content-type: text/javascript'); die(cf_json_encode(array('success' => true, 'data' => $all_results, 'cached' => false))); } else { switch ($_GET['data_type']) { case 'visits': $parameters['dimensions'] = 'ga:date,ga:medium'; $parameters['metrics'] = 'ga:visits,ga:bounces,ga:entrances,ga:pageviews,ga:newVisits,ga:timeOnSite'; //$parameters['filters'] = 'ga:medium==referral,ga:medium==organic,ga:medium==email,ga:medium==cpc'; //$parameters['sort'] = '-ga:visits'; break; case 'geo': $parameters['dimensions'] = 'ga:country'; $parameters['metrics'] = 'ga:visits'; $parameters['sort'] = '-ga:visits'; break; case 'top_referrals': $parameters['dimensions'] = 'ga:medium,ga:source'; $parameters['metrics'] = 'ga:visits,ga:timeOnSite,ga:pageviews'; $parameters['sort'] = '-ga:visits'; $parameters['filters'] = 'ga:medium==referral,ga:medium==organic,ga:medium==email,ga:medium==cpc'; break; case 'referral_media': $parameters['dimensions'] = 'ga:medium'; $parameters['metrics'] = 'ga:visits'; $parameters['sort'] = '-ga:visits'; break; case 'top_content': $parameters['dimensions'] = 'ga:pagePath'; $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits'; $parameters['sort'] = '-ga:pageviews'; break; case 'keywords': $parameters['dimensions'] = 'ga:keyword'; $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits'; $parameters['sort'] = '-ga:pageviews'; $parameters['filters'] = 'ga:source==' . $_GET['source_name']; break; case 'referral_paths': $parameters['dimensions'] = 'ga:source,ga:referralPath'; $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits'; $parameters['sort'] = '-ga:pageviews'; $parameters['filters'] = 'ga:source==' . $_GET['source_name']; break; case 'email_referrals': $parameters['dimensions'] = 'ga:campaign'; $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits'; $parameters['sort'] = '-ga:pageviews'; $parameters['filters'] = 'ga:medium==email'; break; default: break; } $ch = curl_init('https://www.google.com/analytics/feeds/data?' . http_build_query($parameters)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $a360_ga_token . '"')); $result = curl_exec($ch); } if (!$result) { header('Content-type: text/javascript'); die(cf_json_encode(array('success' => false, 'error' => curl_error($ch)))); } $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (substr($http_code, 0, 1) == '2') { $result = a360_reportObjectMapper($result); header('Content-type: text/javascript'); die(cf_json_encode(array('success' => true, 'data' => $result, 'cached' => false))); } else { header('Content-type: text/javascript'); die(cf_json_encode(array('success' => false, 'error' => $result))); } break; } } if (!empty($_POST['a360_action']) && current_user_can('manage_options')) { switch ($_POST['a360_action']) { case 'update_mc_api_key': if (isset($_POST['a360_username']) && isset($_POST['a360_password'])) { $key_result = a360_fetch_API_key($_POST['a360_username'], $_POST['a360_password']); if ($key_result['success']) { delete_option('a360_chimp_chatter_url'); update_option('a360_api_key', $key_result['api_key']); $q = http_build_query(array('updated' => 'true')); } else { $q = http_build_query(array('a360_error' => $key_result['error'])); } } wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . $q); die; break; case 'clear_mc_api_key': delete_option('a360_api_key'); delete_option('a360_chimp_chatter_url'); wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . http_build_query(array('updated' => 'true'))); break; case 'revoke_ga_token': global $a360_ga_token; $ch = curl_init('https://www.google.com/accounts/AuthSubRevokeToken'); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $a360_ga_token . '"')); $result = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 200) { delete_option('a360_ga_token'); delete_option('a360_ga_profile_id'); wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&update=true'); } else { wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . http_build_query(array('a360_error' => 'Could not revoke token!'))); } break; case 'set_ga_profile_id': $result = update_option('a360_ga_profile_id', $_POST['profile_id']); wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&updated=true'); break; } } }
function campaign_archive() { $api_key = $this->EE->TMPL->fetch_param('api_key'); $list_id = $this->EE->TMPL->fetch_param('list_id'); $folder_id = $this->EE->TMPL->fetch_param('folder_id'); $status = $this->EE->TMPL->fetch_param('status'); $limit = $this->EE->TMPL->fetch_param('limit'); if (!$api_key) { return; } $api = new MCAPI($api_key); $filters = ""; // Apply by list filter if ($list_id) { $filters['list_id'] = $list_id; } // Apply by folder filter if ($folder_id) { $filters['folder_id'] = $folder_id; } // Apply by status filter if ($status) { $filters['status'] = $status; } $result = $api->campaigns($filters); if ($api->errorCode) { $this->EE->TMPL->log_item('Mailchimp EE: Campaign Archive failed, Mailchimp API complained - ' . $api->errorMessage . ' (' . $api->errorCode . ')'); } // Make sure there is at least one campaign if (count($result) == 0) { return; } // Apply limit if ($limit) { $result = array_slice($result, 0, $limit); } $vars = $result; // Convert date values into timestamps foreach ($vars as $key => $value) { $vars[$key]['send_time'] = strtotime($value['send_time']); $vars[$key]['create_time'] = strtotime($value['create_time']); } return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars); }