gads_dash_content

The Google Adsense Dashboard gads dash content function.

Description

gads_dash_content(); 

Usage

  1. if ( !function_exists( 'gads_dash_content' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-adsense-dashboard/gads_dash.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = gads_dash_content(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/gads_dash.php  
  1. function gads_dash_content() { 
  2. require_once 'functions.php'; 
  3.  
  4. $auth = new AdSenseAuth (); 
  5.  
  6. $result = $auth->authenticate ( 'default' ); 
  7.  
  8. $adSense = $auth->getAdSenseService (); 
  9.  
  10. if (isset ( $_REQUEST ['query_adsense'] )) 
  11. $query_adsense = $_REQUEST ['query_adsense']; 
  12. else 
  13. $query_adsense = "EARNINGS"; 
  14.  
  15. if (isset ( $_REQUEST ['period_adsense'] )) 
  16. $period_adsense = $_REQUEST ['period_adsense']; 
  17. else 
  18. $period_adsense = "last30days"; 
  19.  
  20. if (get_option ( 'gads_dash_style' ) == "light") { 
  21. $css = "colors:['gray', 'darkgray'], "; 
  22. $colors = "black"; 
  23. } else { 
  24. $css = "colors:['green', 'darkgreen'], "; 
  25. $colors = "green"; 
  26.  
  27. switch ($period_adsense) { 
  28.  
  29. case 'today' : 
  30. $from = date ( 'Y-m-d' ); 
  31. $to = date ( 'Y-m-d' ); 
  32. break; 
  33.  
  34. case 'yesterday' : 
  35. $from = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  36. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  37. break; 
  38.  
  39. case 'last7days' : 
  40. $from = date ( 'Y-m-d', time () - 7 * 24 * 60 * 60 ); 
  41. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  42. break; 
  43.  
  44. case 'last14days' : 
  45. $from = date ( 'Y-m-d', time () - 14 * 24 * 60 * 60 ); 
  46. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  47. break; 
  48.  
  49. default : 
  50. $from = date ( 'Y-m-d', time () - 30 * 24 * 60 * 60 ); 
  51. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  52. break; 
  53.  
  54. switch ($query_adsense) { 
  55.  
  56. case 'COST_PER_CLICK' : 
  57. $title = "Cost per Click"; 
  58. break; 
  59.  
  60. case 'CLICKS' : 
  61. $title = "Clicks"; 
  62. break; 
  63.  
  64. case 'PAGE_VIEWS' : 
  65. $title = "Page Views"; 
  66. break; 
  67.  
  68. case 'PAGE_VIEWS_CTR' : 
  69. $title = "Click Through Rate"; 
  70. break; 
  71.  
  72. case 'PAGE_VIEWS_RPM' : 
  73. $title = "Revenue per thousand impressions"; 
  74. break; 
  75.  
  76. default : 
  77. $title = "Earnings"; 
  78.  
  79. $optParams = array ( 
  80. 'metric' => array ( 
  81. $query_adsense  
  82. ),  
  83. 'dimension' => 'DATE',  
  84. 'sort' => 'DATE',  
  85. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  86. ); 
  87. try { 
  88.  
  89. $serial = 'gadsdash_qr1' . str_replace ( array ( 
  90. ', ',  
  91. '-',  
  92. date ( 'Y' )  
  93. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) . $query_adsense ); 
  94. $transient = get_transient ( $serial ); 
  95. if (empty ( $transient )) { 
  96. $data = $adSense->reports->generate ( $from, $to, $optParams ); 
  97. set_transient ( $serial, $data, get_option ( 'gads_dash_cachetime' ) ); 
  98. } else { 
  99. $data = $transient; 
  100. } catch ( exception $e ) { 
  101. if (get_option ( 'gads_dash_token' )) { 
  102. echo gads_dash_pretty_error ( $e ); 
  103. return; 
  104.  
  105. if (! isset ( $data )) { 
  106. return; 
  107.  
  108. $gads_chart1_data = ""; 
  109. for($i = 0; $i < $data ['totalMatchedRows']; $i ++) { 
  110. if ($query_adsense == 'PAGE_VIEWS_CTR') 
  111. $gads_chart1_data .= "['" . $data ['rows'] [$i] [0] . "', " . ($data ['rows'] [$i] [1] * 100) . "], "; 
  112. else 
  113. $gads_chart1_data .= "['" . $data ['rows'] [$i] [0] . "', " . $data ['rows'] [$i] [1] . "], "; 
  114.  
  115. $gads_chart1_data = wp_kses ( rtrim ( $gads_chart1_data, ', ' ), '' ); 
  116.  
  117. $optParams = array ( 
  118. 'metric' => array ( 
  119. 'EARNINGS',  
  120. 'COST_PER_CLICK',  
  121. 'CLICKS',  
  122. 'PAGE_VIEWS',  
  123. 'PAGE_VIEWS_CTR',  
  124. 'PAGE_VIEWS_RPM'  
  125. ),  
  126. 'dimension' => 'YEAR',  
  127. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  128. ); 
  129.  
  130. try { 
  131. $serial = 'gadsdash_qr2' . str_replace ( array ( 
  132. ', ',  
  133. '-',  
  134. date ( 'Y' )  
  135. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) ); 
  136. $transient = get_transient ( $serial ); 
  137. if (empty ( $transient )) { 
  138. $gads_sum_data = $adSense->reports->generate ( $from, $to, $optParams ); 
  139. set_transient ( $serial, $gads_sum_data, get_option ( 'gads_dash_cachetime' ) ); 
  140. } else { 
  141. $gads_sum_data = $transient; 
  142. } catch ( exception $e ) { 
  143. if (get_option ( 'gads_dash_token' )) { 
  144. echo gads_dash_pretty_error ( $e ); 
  145. return; 
  146.  
  147. // Channel Performance 
  148.  
  149. $optParams = array ( 
  150. 'metric' => array ( 
  151. 'EARNINGS',  
  152. 'COST_PER_CLICK',  
  153. 'CLICKS',  
  154. 'PAGE_VIEWS',  
  155. 'PAGE_VIEWS_CTR',  
  156. 'PAGE_VIEWS_RPM'  
  157. ),  
  158. 'dimension' => 'CUSTOM_CHANNEL_NAME',  
  159. 'sort' => '-EARNINGS',  
  160. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  161. ); 
  162. try { 
  163.  
  164. $serial = 'gadsdash_qr3' . str_replace ( array ( 
  165. ', ',  
  166. '-',  
  167. date ( 'Y' )  
  168. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) ); 
  169. $transient = get_transient ( $serial ); 
  170. if (empty ( $transient )) { 
  171. $chdata = $adSense->reports->generate ( $from, $to, $optParams ); 
  172. set_transient ( $serial, $chdata, get_option ( 'gads_dash_cachetime' ) ); 
  173. } else { 
  174. $chdata = $transient; 
  175. } catch ( exception $e ) { 
  176. if (get_option ( 'gads_dash_token' )) { 
  177. echo gads_dash_pretty_error ( $e ); 
  178. return; 
  179. // print_r($chdata); 
  180. $gads_ch_data = ""; 
  181. for($i = 0; $i < $chdata ['totalMatchedRows']; $i ++) { 
  182. $validate_rpm = ($chdata ['rows'] [$i] [6] == "") ? 0.00 : $chdata ['rows'] [$i] [6]; 
  183. $gads_ch_data .= "['" . $chdata ['rows'] [$i] [0] . "', " . $chdata ['rows'] [$i] [1] . ", " . $chdata ['rows'] [$i] [2] . ", " . $chdata ['rows'] [$i] [3] . ", " . $chdata ['rows'] [$i] [4] . ", " . ($chdata ['rows'] [$i] [5] * 100) . ", " . $validate_rpm . "], "; 
  184. $gads_ch_data = wp_kses ( rtrim ( $gads_ch_data, ', ' ), '' ); 
  185.  
  186. // Ads Performance 
  187.  
  188. $optParams = array ( 
  189. 'metric' => array ( 
  190. 'EARNINGS',  
  191. 'COST_PER_CLICK',  
  192. 'CLICKS',  
  193. 'AD_REQUESTS',  
  194. 'AD_REQUESTS_CTR',  
  195. 'AD_REQUESTS_RPM'  
  196. ),  
  197. 'dimension' => 'AD_UNIT_NAME',  
  198. 'sort' => '-EARNINGS',  
  199. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  200. ); 
  201. try { 
  202.  
  203. $serial = 'gadsdash_qr4' . str_replace ( array ( 
  204. ', ',  
  205. '-',  
  206. date ( 'Y' )  
  207. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) ); 
  208. $transient = get_transient ( $serial ); 
  209. if (empty ( $transient )) { 
  210. $adsdata = $adSense->reports->generate ( $from, $to, $optParams ); 
  211. set_transient ( $serial, $adsdata, get_option ( 'gads_dash_cachetime' ) ); 
  212. } else { 
  213. $adsdata = $transient; 
  214. } catch ( exception $e ) { 
  215. if (get_option ( 'gads_dash_token' )) { 
  216. echo gads_dash_pretty_error ( $e ); 
  217. return; 
  218. // print_r($adsdata); 
  219. $gads_ads_data = ""; 
  220. for($i = 0; $i < $adsdata ['totalMatchedRows']; $i ++) { 
  221. $gads_ads_data .= "['" . $adsdata ['rows'] [$i] [0] . "', " . $adsdata ['rows'] [$i] [1] . ", " . $adsdata ['rows'] [$i] [2] . ", " . $adsdata ['rows'] [$i] [3] . ", " . $adsdata ['rows'] [$i] [4] . ", " . ($adsdata ['rows'] [$i] [5] * 100) . ", " . $adsdata ['rows'] [$i] [6] . "], "; 
  222. $gads_ads_data = wp_kses ( rtrim ( $gads_ads_data, ', ' ), '' ); 
  223.  
  224. $code = '<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
  225. <script type="text/javascript"> 
  226. google.load("visualization", "1", {packages:["corechart"]}); 
  227. google.setOnLoadCallback(gads_dash_callback); 
  228.  
  229. function gads_dash_callback() { 
  230. adsense_drawChart1(); 
  231. if(typeof gads_dash_channels == "function") { 
  232. gads_dash_channels(); 
  233. if(typeof gads_dash_ads == "function") { 
  234. gads_dash_ads(); 
  235. }  
  236.  
  237. function adsense_drawChart1() { 
  238. var data = google.visualization.arrayToDataTable([' . " 
  239. ['Date', '" . $title . "'], " . $gads_chart1_data . "  
  240. ]); 
  241.  
  242. var options = { 
  243. legend: {position: 'none'},  
  244. pointSize: 3, ".$css." 
  245. title: '".$title."',  
  246. chartArea: {width: '99%', height: '90%'},  
  247. vAxis: { textPosition: 'in', minValue: 0},  
  248. hAxis: { textPosition: 'none' } 
  249. }; 
  250.  
  251. var chart = new google.visualization.AreaChart(document.getElementById('adsense_chart1_div')); 
  252. chart.draw(data, options); 
  253.  
  254. }"; 
  255.  
  256. if (get_option ( 'gads_dash_channels' )) { 
  257.  
  258. if ($gads_ch_data) { 
  259. $code .= ' 
  260. google.load("visualization", "1", {packages:["table"]}) 
  261. function gads_dash_channels() { 
  262. var data = google.visualization.arrayToDataTable([' . " 
  263. ['" . __ ( "Channel", 'gads-dash' ) . "', '" . __ ( "Earnings", 'gads-dash' ) . "', '" . __ ( "CPC", 'gads-dash' ) . "', '" . __ ( "Clicks", 'gads-dash' ) . "', '" . __ ( "Views", 'gads-dash' ) . "', '" . __ ( "CTR", 'gads-dash' ) . "', '" . __ ( "RPM", 'gads-dash' ) . "'], " . $gads_ch_data . "  
  264. ]); 
  265.  
  266. var options = { 
  267. page: 'enable',  
  268. pageSize: 5,  
  269. width: '100%' 
  270. };  
  271.  
  272. var chart = new google.visualization.Table(document.getElementById('gads_dash_channels')); 
  273. chart.draw(data, options); 
  274.  
  275. }"; 
  276.  
  277. if (get_option ( 'gads_dash_ads' )) { 
  278.  
  279. if ($gads_ads_data) { 
  280. $code .= ' 
  281. google.load("visualization", "1", {packages:["table"]}) 
  282. function gads_dash_ads() { 
  283. var data = google.visualization.arrayToDataTable([' . " 
  284. ['" . __ ( "Ad Slot", 'gads-dash' ) . "', '" . __ ( "Earnings", 'gads-dash' ) . "', '" . __ ( "CPC", 'gads-dash' ) . "', '" . __ ( "Clicks", 'gads-dash' ) . "', '" . __ ( "Requests", 'gads-dash' ) . "', '" . __ ( "CTR", 'gads-dash' ) . "', '" . __ ( "RPM", 'gads-dash' ) . "'], " . $gads_ads_data . "  
  285. ]); 
  286.  
  287. var options = { 
  288. page: 'enable',  
  289. pageSize: 5,  
  290. width: '100%' 
  291. };  
  292.  
  293. var chart = new google.visualization.Table(document.getElementById('gads_dash_ads')); 
  294. chart.draw(data, options); 
  295.  
  296. }"; 
  297. $btn_class = (get_option ( 'gads_dash_style' ) == 'green') ? 'gadsbutton' : 'button button-secondary'; 
  298. $code .= "</script>" . ' 
  299. <div id="gads-dash"> 
  300. <center> 
  301. <div id="buttons_div_adsense"> 
  302. <center> 
  303. <input class="' . $btn_class . '" type="button" value="' . __ ( "Today", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=today&query_adsense=' . $query_adsense . '\'" /> 
  304. <input class="' . $btn_class . '" type="button" value="' . __ ( "Yesterday", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=yesterday&query_adsense=' . $query_adsense . '\'" /> 
  305. <input class="' . $btn_class . '" type="button" value="' . __ ( "7 Days", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=last7days&query_adsense=' . $query_adsense . '\'" /> 
  306. <input class="' . $btn_class . '" type="button" value="' . __ ( "14 Days", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=last14days&query_adsense=' . $query_adsense . '\'" /> 
  307. <input class="' . $btn_class . '" type="button" value="' . __ ( "30 Days", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=last30days&query_adsense=' . $query_adsense . '\'" /> 
  308. </center> 
  309. </div> 
  310.  
  311. <div id="adsense_chart1_div"></div> 
  312.  
  313. <div id="adsense_details_div"> 
  314. <table class="adsensetable" cellpadding="4"> 
  315. <tr> 
  316. <td width=="24%">' . __ ( "Earnings:", 'gads-dash' ) . '</td> 
  317. <td width="12%" class="adsensevalue"><a href="?query_adsense=EARNINGS&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) $gads_sum_data ['rows'] [0] [0] . '</td> 
  318. <td width="30%">' . __ ( "Views:", 'gads-dash' ) . '</td> 
  319. <td width="12%" class="adsensevalue"><a href="?query_adsense=PAGE_VIEWS&period_adsense=' . $period_adsense . '" class="adsensetable">' . (int) $gads_sum_data ['rows'] [0] [3] . '</a></td> 
  320. <td width="24%">' . __ ( "Clicks:", 'gads-dash' ) . '</td> 
  321. <td width="12%" class="adsensevalue"><a href="?query_adsense=CLICKS&period_adsense=' . $period_adsense . '" class="adsensetable">' . (int) $gads_sum_data ['rows'] [0] [2] . '</a></td> 
  322. </tr> 
  323. <tr> 
  324. <td>' . __ ( "CPC:", 'gads-dash' ) . '</td> 
  325. <td class="adsensevalue"><a href="?query_adsense=COST_PER_CLICK&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) $gads_sum_data ['rows'] [0] [1] . '</a></td> 
  326. <td>' . __ ( "CTR:", 'gads-dash' ) . '</td> 
  327. <td class="adsensevalue"><a href="?query_adsense=PAGE_VIEWS_CTR&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) ($gads_sum_data ['rows'] [0] [4] * 100) . '</a></td> 
  328. <td>' . __ ( "RPM:", 'gads-dash' ) . '</td> 
  329. <td class="adsensevalue"><a href="?query_adsense=PAGE_VIEWS_RPM&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) $gads_sum_data ['rows'] [0] [5] . '</a></td> 
  330. </tr> 
  331. </table> 
  332. </div> 
  333. </center> 
  334. </div>'; 
  335.  
  336. if (get_option ( 'gads_dash_channels' )) 
  337. $code .= '<br /><br /><div id="gads_dash_channels"></div>'; 
  338.  
  339. if (get_option ( 'gads_dash_ads' )) 
  340. $code .= '<br /><div id="gads_dash_ads"></div>'; 
  341.  
  342. echo $code;