MonsterInsights_Report_Overview

The Google Analytics for WordPress by MonsterInsights MonsterInsights Report Overview class.

Defined (1)

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

/includes/admin/reports/overview.php  
  1. final class MonsterInsights_Report_Overview extends MonsterInsights_Report { 
  2.  
  3. public $report_name; 
  4.  
  5. public $report_hook; 
  6.  
  7. /** 
  8. * Primary class constructor. 
  9. * @access public 
  10. * @since 6.0.0 
  11. */ 
  12. public function __construct() { 
  13. $this->report_name = __( 'Overview', 'google-analytics-for-wordpress' ); 
  14. $this->report_hook = 'overview'; 
  15.  
  16. parent::__construct(); 
  17.  
  18. // Adds/Refreshes the data 
  19. public function add_report_data( $client, $id ) { 
  20. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  21. return false; 
  22.  
  23. $dates = $this->get_date_range(); 
  24.  
  25. // Pageviews 
  26. $pageviews = $client->do_request( 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A' . $id . '&start-date=' . $dates['start'] . '&end-date=' . $dates['end'] . '&metrics=ga%3Apageviews&dimensions=ga%3Adate&max-results=' . $this->get_api_max_limit() ); 
  27. $pageviews = $this->parse_request( $pageviews ); 
  28. update_option( 'monsterinsights_report_overview_pageviews', $pageviews ); 
  29.  
  30. // Top posts and pages 
  31. $top_content = $client->do_request( 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A' . $id . '&start-date=' . $dates['start'] . '&end-date=' . $dates['end'] . '&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews&max-results=' . $this->get_api_max_limit() ); 
  32. $top_content = $this->parse_request( $top_content ); 
  33. update_option( 'monsterinsights_report_overview_top_content', $top_content ); 
  34.  
  35. // Top sources 
  36. $top_sources = $client->do_request( 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A' . $id . '&start-date=' . $dates['start'] . '&end-date=' . $dates['end'] . '&metrics=ga%3Apageviews&dimensions=ga%3Asource&sort=-ga%3Apageviews&max-results=' . $this->get_api_max_limit() ); 
  37. $top_sources = $this->parse_request( $top_sources ); 
  38. update_option( 'monsterinsights_report_overview_top_sources', $top_sources ); 
  39.  
  40. // Top countries 
  41. $countries = $client->do_request( 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A' . $id . '&start-date=' . $dates['start'] . '&end-date=' . $dates['end'] . '&metrics=ga%3Apageviews&dimensions=ga%3AcountryIsoCode&sort=-ga%3Apageviews&max-results=' . $this->get_api_max_limit() ); 
  42. $countries = $this->parse_request( $countries ); 
  43. update_option( 'monsterinsights_report_overview_countries', $countries ); 
  44.  
  45. monsterinsights_update_option( 'cron_last_run', time() ); 
  46.  
  47. // Gets the data 
  48. public function get_report_data( ) { 
  49. $data = array(); 
  50. $data['pageviews'] = get_option( 'monsterinsights_report_overview_pageviews', array() ); 
  51. $data['top-content'] = get_option( 'monsterinsights_report_overview_top_content', array() ); 
  52. $data['top-sources'] = get_option( 'monsterinsights_report_overview_top_sources', array() ); 
  53. $data['countries'] = get_option( 'monsterinsights_report_overview_countries', array() ); 
  54. return $data; 
  55.  
  56. // Removes report data 
  57. public function delete_report_data() { 
  58. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  59. return false; 
  60.  
  61. delete_option( 'monsterinsights_report_overview_pageviews' ); 
  62. delete_option( 'monsterinsights_report_overview_top_content' ); 
  63. delete_option( 'monsterinsights_report_overview_top_sources' ); 
  64. delete_option( 'monsterinsights_report_overview_countries' ); 
  65. return true; 
  66.  
  67. // Outputs the report. 
  68. public function show_report( ) { 
  69. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  70. return false; 
  71. $data = $this->get_report_data(); 
  72.  
  73. $pageviews = ! empty( $data['pageviews'] ) ? $data['pageviews'] : false; 
  74. $top = ! empty( $data['top-content'] ) ? $data['top-content'] : false; 
  75. $sources = ! empty( $data['top-sources'] ) ? $data['top-sources'] : false; 
  76. $countries = ! empty( $data['countries'] ) ? $data['countries'] : false; 
  77. ?> 
  78. <?php 
  79. if ( empty( $pageviews ) && empty( $top ) && empty( $sources ) && empty( $countries ) ) { 
  80. echo MonsterInsights()->notices->display_inline_notice( 'monsterinsights_standard_notice', '', __( 'If you\'ve just installed MonsterInsights, data may take up to 24 hours to populate here. Check back soon!', 'google-analytics-for-wordpress'), 'notice', false, array() ); 
  81.  
  82. if ( ! empty( $pageviews ) ) { 
  83. $pageviews_labels = array(); 
  84. $pageviews_datapoints = array(); 
  85. $max = 0; 
  86. foreach ( $pageviews['data'] as $pageviews_index => $pageviews_values ) { 
  87. $pageviews_labels[] = "'" . esc_js( date_i18n( 'j M', strtotime( $pageviews_values[0] ) ) ) . "'";  
  88. $pageviews_datapoints[] = esc_js( $pageviews_values[1] ); 
  89. if ( $max < $pageviews_values[1] ) { 
  90. $max = $pageviews_values[1]; 
  91.  
  92. if ( $max >= 1 ) {  
  93. ?> 
  94. <div class="monsterinsights-grid"> 
  95. <div class="monsterinsights-col-1-1 monsterinsights-grid-grey-bg monsterinsights-grid-border"> 
  96. <div class="monsterinsights-reports-box-title"> 
  97. <?php esc_html_e( 'PAGE VIEWS', 'google-analytics-for-wordpress' ) ?> 
  98. </div> 
  99. <div class="monsterinsights-reports-box-datagraph" style="position:relative;"> 
  100. <canvas id="monsterinsights-overview-pageviews" width="400px" height="400px"></canvas> 
  101. <?php 
  102. $up = MONSTERINSIGHTS_PLUGIN_URL . 'assets/images/up.png'; 
  103. $up2x = MONSTERINSIGHTS_PLUGIN_URL . 'assets/images/up@2x.png'; 
  104. $down = MONSTERINSIGHTS_PLUGIN_URL . 'assets/images/down.png'; 
  105. $down2x = MONSTERINSIGHTS_PLUGIN_URL . 'assets/images/down@2x.png'; 
  106. $uplabel = esc_attr__( 'UP', 'google-analytics-for-wordpress' ); 
  107. $downlabel = esc_attr__( 'DOWN', 'google-analytics-for-wordpress' ); 
  108. ?> 
  109. <script> 
  110. jQuery(document).ready(function() { 
  111. if ( window.uorigindetected != null) { 
  112. var ctx = document.getElementById("monsterinsights-overview-pageviews"); 
  113. var data = { 
  114. labels: [<?php echo implode( ', ', $pageviews_labels ); ?>],  
  115. datasets: [ 
  116. lineTension: 0,  
  117. backgroundColor: 'rgba(236, 249, 246, .5)',  
  118. borderColor: "#47c2a5",  
  119. fillColor : "#ecf9f6",  
  120. pointRadius: 5,  
  121. pointHoverRadius: 5,  
  122. pointBorderColor : "#fff",  
  123. pointBackgroundColor : "#489be8",  
  124. data: [<?php echo implode( ', ', $pageviews_datapoints ); ?>],  
  125.  
  126. }; 
  127.  
  128. Chart.defaults.global.tooltips.custom = function(tooltip) { 
  129. // Tooltip Element 
  130. var tooltipEl = jQuery('#monsterinsights-chartjs-tooltip'); 
  131. if (!tooltipEl[0]) { 
  132. jQuery('body').append('<div id="monsterinsights-chartjs-tooltip"></div>'); 
  133. tooltipEl = jQuery('#monsterinsights-chartjs-tooltip'); 
  134. // Hide if no tooltip 
  135. if (!tooltip.opacity) { 
  136. tooltipEl.css({ 
  137. opacity: 0 
  138. }); 
  139. jQuery('.chartjs-wrap canvas').each(function(index, el) { 
  140. jQuery(el).css('cursor', 'default'); 
  141. }); 
  142. return; 
  143. jQuery(this._chart.canvas).css('cursor', 'pointer'); 
  144.  
  145. // Set caret Position 
  146. tooltipEl.removeClass('above below no-transform'); 
  147. if (tooltip.yAlign) { 
  148. tooltipEl.addClass(tooltip.yAlign); 
  149. } else { 
  150. tooltipEl.addClass('no-transform'); 
  151. // Set Text 
  152. var title = ''; 
  153. if ( tooltip.title[0] == 0 ) { 
  154. title = '0%'; 
  155. } else if ( tooltip.title[0] > 0 ) { 
  156. title = '<img src="<?php echo $up; ?>" srcset="<?php echo $up2x; ?> 2x" alt="<?php echo $uplabel; ?>" style="margin-right:6px"/>' + tooltip.title[1]; 
  157. } else { 
  158. title = '<img src="<?php echo $down; ?>" srcset="<?php echo $down2x; ?> 2x" alt="<?php echo $downlabel; ?>" style="margin-right:6px"/>' + Math.abs( tooltip.title[1] ); 
  159. tooltipEl.html(title); 
  160.  
  161. // Find Y Location on page 
  162. var top = 0; 
  163.  
  164. if (tooltip.yAlign) { 
  165. var ch = 0; 
  166. if (tooltip.caretHeight) { 
  167. ch = tooltip.caretHeight; 
  168. if (tooltip.yAlign == 'above') { 
  169. top = tooltip.y - ch - tooltip.caretPadding; 
  170. } else { 
  171. top = tooltip.y + ch + tooltip.caretPadding; 
  172.  
  173. // Display, position, and set styles for font 
  174. tooltipEl.css({ 
  175. opacity: 1,  
  176. width: tooltip.width ? (tooltip.width + 'px') : 'auto',  
  177. left: 30 + tooltip.x + 'px',  
  178. top: top - 8 +'px',  
  179. fontFamily: tooltip._fontFamily,  
  180. fontSize: tooltip.fontSize,  
  181. fontStyle: tooltip._fontStyle,  
  182. padding: tooltip.yPadding + 'px ' + tooltip.xPadding + 'px',  
  183. }); 
  184. }; 
  185.  
  186. var MonsterInsightsOverviewPageviews = new Chart(ctx, { 
  187. type: 'line',  
  188. data: data,  
  189. options: { 
  190. legend: { 
  191. display: false,  
  192. },  
  193. tooltips: { 
  194. enabled: false,  
  195. yAlign: 'above',  
  196. callbacks: { 
  197. title: function(tooltipItem, data) { 
  198. tooltipItem = tooltipItem[0]; 
  199. var prevvalue = 0; 
  200. if ( tooltipItem.index != 0 ) { 
  201. prevvalue = data.datasets[0].data[tooltipItem.index - 1] 
  202.  
  203. var value = data.datasets[0].data[tooltipItem.index]; 
  204. var change = 0; 
  205. if ( prevvalue == 0 && value == 0 ) { 
  206. change = 0; 
  207. } else if ( prevvalue == 0 ) { 
  208. change = 100; 
  209. } else if ( value == 0 ) { 
  210. change = -100; 
  211. } else { 
  212. change = ((value - prevvalue) / prevvalue) * 100; 
  213. change = Math.round( change ); 
  214. return new Array(change, value); 
  215. },  
  216. label: function(tooltipItem, data) { 
  217. return ''; 
  218. },  
  219. scales: { 
  220. xAxes: [{ 
  221. spanGaps: true,  
  222. position: 'bottom',  
  223. gridLines: { 
  224. show: true,  
  225. color: "#f1f1f1",  
  226. },  
  227. ticks: { 
  228. fontColor: "#7f8591",  
  229. }],  
  230. yAxes: [{ 
  231. gridLines: { 
  232. show: true,  
  233. color: "#f1f1f1",  
  234. },  
  235. ticks: { 
  236. fontColor: "#7f8591",  
  237. callback: function(value) {if (value % 1 === 0) {return value;}} 
  238. }] 
  239. },  
  240. animation: { 
  241. duration: 5000,  
  242. },  
  243. legend : { 
  244. display: false,  
  245. },  
  246. responsive: true,  
  247. maintainAspectRatio: false,  
  248. borderWidth: 1,  
  249. }); 
  250. }); 
  251. </script> 
  252. <div id="monsterinsights-chartjs-tooltip"></div> 
  253. </div> 
  254. </div> 
  255. </div> 
  256. <?php } ?> 
  257. <?php } ?> 
  258. <?php 
  259. if ( ! empty( $top ) && ! empty( $sources ) ) { 
  260. ?> 
  261. <div class="monsterinsights-grid"> 
  262. <div class="monsterinsights-col-1-2"> 
  263. <div class="monsterinsights-datalist-box-title"> 
  264. <?php esc_html_e( 'TOP POSTS AND PAGES', 'google-analytics-for-wordpress' ); ?> 
  265. </div> 
  266. <div class="monsterinsights-grid-border"> 
  267. <div class="monsterinsights-col-1-1 monsterinsights-reports-box-title"> 
  268. <div class="monsterinsights-col-7-8"> 
  269. <?php esc_html_e( 'TITLE', 'google-analytics-for-wordpress' ); ?> 
  270. </div> 
  271. <div class="monsterinsights-col-1-8"> 
  272. <?php esc_html_e( 'VIEWS', 'google-analytics-for-wordpress' ); ?> 
  273. </div> 
  274. </div> 
  275. <div class="monsterinsights-reports-box-datalist"> 
  276. <?php  
  277. $i = 0; 
  278. foreach ( $top['data'] as $top_index => $top_values ) { ?> 
  279. <?php 
  280. if ( $i === 10 ) { // Limit to 10 max 
  281. break; 
  282. } else { 
  283. $i++; 
  284. $title = isset( $top_values[0] ) ? esc_html( $top_values[0] ) : esc_html__( 'Unknown Page or Post', 'google-analytics-for-wordpress' ); 
  285. $views = isset( $top_values[1] ) ? absint( $top_values[1] ) : 0; 
  286. ?> 
  287. <div class="monsterinsights-data-row"> 
  288. <div class="monsterinsights-col-7-8"><?php echo $title ?></div> 
  289. <div class="monsterinsights-col-1-8"><?php echo $views ?></div> 
  290. </div> 
  291. <?php }  
  292. for ( $i; $i < 10; $i++ ) { // if we have less than 10, make empty rows 
  293. ?> 
  294. <div class="monsterinsights-data-row"> 
  295. <div class="monsterinsights-col-7-8"> </div> 
  296. <div class="monsterinsights-col-1-8"> </div> 
  297. </div> 
  298. <?php 
  299. ?> 
  300. </div> 
  301. <div style="clear: both;"></div> 
  302. </div> 
  303. </div> 
  304.  
  305. <div class="monsterinsights-col-1-2"> 
  306. <div class="monsterinsights-datalist-box-title"> 
  307. <?php esc_html_e( 'TOP TRAFFIC SOURCES', 'google-analytics-for-wordpress' ) ?> 
  308. </div> 
  309. <div class="monsterinsights-grid-border"> 
  310. <div class="monsterinsights-col-1-1 monsterinsights-reports-box-title"> 
  311. <div class="monsterinsights-col-7-8"> 
  312. <?php esc_html_e( 'SITE', 'google-analytics-for-wordpress' ); ?> 
  313. </div> 
  314. <div class="monsterinsights-col-1-8"> 
  315. <?php esc_html_e( 'VIEWS', 'google-analytics-for-wordpress' ); ?> 
  316. </div> 
  317. </div> 
  318. <div class="monsterinsights-reports-box-datalist"> 
  319. <?php  
  320. $i = 0; 
  321. foreach ( $sources['data'] as $sources_index => $sources_values ) { ?> 
  322. <?php 
  323. if ( $i === 10 ) { // Limit to 10 max 
  324. break; 
  325. } else { 
  326. $i++; 
  327. $title = isset( $sources_values[0] ) ? esc_html( $sources_values[0] ) : esc_html__( 'Unknown Source', 'google-analytics-for-wordpress' ); 
  328. $views = isset( $sources_values[1] ) ? absint( $sources_values[1] ) : 0; 
  329. ?> 
  330. <div class="monsterinsights-data-row"> 
  331. <div class="monsterinsights-col-7-8"><?php echo $title ?></div> 
  332. <div class="monsterinsights-col-1-8"><?php echo $views ?></div> 
  333. </div> 
  334. <?php }  
  335. for ( $i; $i < 10; $i++ ) { // if we have less than 10, make empty rows 
  336. ?> 
  337. <div class="monsterinsights-data-row"> 
  338. <div class="monsterinsights-col-7-8"> </div> 
  339. <div class="monsterinsights-col-1-8"> </div> 
  340. </div> 
  341. <?php 
  342. ?> 
  343. </div> 
  344. <div style="clear: both;"></div> 
  345. </div> 
  346. </div> 
  347. </div> 
  348. <?php } ?> 
  349. <?php 
  350. if ( ! empty( $countries ) ) { 
  351. ?> 
  352. <div class="monsterinsights-grid"> 
  353. <div class="monsterinsights-col-1-1 monsterinsights-grid-border monsterinsights-clearfix"> 
  354. <div class="monsterinsights-col-1-1 monsterinsights-reports-box-title"> 
  355. <div class="monsterinsights-col-1-1"> 
  356. <?php esc_html_e( 'TOP COUNTRIES', 'google-analytics-for-wordpress' ); ?> 
  357. </div> 
  358. </div> 
  359. <div class="monsterinsights-col-1-1 monsterinsights-no-padding-right"> 
  360. <div id="monsterinsights-reports-country-map" style="height: 400px;" ></div> 
  361. <script type="text/javascript"> 
  362. jQuery(function() { 
  363. if ( window.uorigindetected != null) { 
  364. var viewname = <?php echo "' " . __( 'views', 'google-analytics-for-wordpress' ) . "'"; ?>; 
  365. var monsterinsights_countries = <?php echo $this->get_countries_array( $countries ) ?>; 
  366. jQuery('#monsterinsights-reports-country-map').vectorMap({ 
  367. map: 'world_mill',  
  368. series: { 
  369. regions: [{ 
  370. values: monsterinsights_countries,  
  371. scale: ['#FFFFFF', '#0071A4'],  
  372. normalizeFunction: 'polynomial' 
  373. }] 
  374. },  
  375. onRegionTipShow: function(e, el, code) { 
  376. el.html(el.html()+' ('+ monsterinsights_countries[code] + viewname +')'); 
  377. }); 
  378. }); 
  379. </script> 
  380. </div> 
  381. <div class="monsterinsights-col-1-1 monsterinsights-reports-box-title monsterinsights-clearfix"> 
  382. <div class="monsterinsights-col-1-2"> 
  383. <?php esc_html_e( 'COUNTRY', 'google-analytics-for-wordpress' ); ?> 
  384. </div> 
  385. <div class="monsterinsights-col-1-4"> 
  386. <?php esc_html_e( 'VIEWS', 'google-analytics-for-wordpress' ); ?> 
  387. </div> 
  388. <div class="monsterinsights-col-1-4"> 
  389. <?php esc_html_e( '%', 'google-analytics-for-wordpress' ); ?> 
  390. </div> 
  391. </div> 
  392.  
  393. <div class="monsterinsights-reports-box-datalist"> 
  394. <?php  
  395. $i = 0; 
  396. $list_of_countries = monsterinsights_get_country_list( true ); 
  397. foreach ( $countries['data'] as $countries_index => $countries_values ) { ?> 
  398. <?php 
  399. if ( $i === 10 ) { // Limit to 10 max 
  400. break; 
  401. } else { 
  402. $i++; 
  403. $title = isset( $countries_values[0] ) ? esc_html( $countries_values[0] ) : __( 'Country not set', 'google-analytics-for-wordpress' ); 
  404. $title = isset( $list_of_countries[ $title ] ) ? esc_html( $list_of_countries[ $title ] ) : $title; 
  405. $views = isset( $countries_values[1] ) ? absint( $countries_values[1] ) : 0; 
  406. $percent = ! empty( $countries['total']['ga:pageviews'] ) && absint( $countries['total']['ga:pageviews'] ) > 0 ? $views / absint( $countries['total']['ga:pageviews'] ) : 0; 
  407. $percent = number_format( $percent * 100, 2 ) . '%'; 
  408. ?> 
  409. <div class="monsterinsights-data-row"> 
  410. <div class="monsterinsights-col-1-2"><?php echo $title ?></div> 
  411. <div class="monsterinsights-col-1-4"><?php echo $views ?></div> 
  412. <div class="monsterinsights-col-1-4"><?php echo $percent ?></div> 
  413. </div> 
  414. <?php } 
  415. ?> 
  416. </div> 
  417. </div> 
  418. </div> 
  419. <?php } ?> 
  420. <?php 
  421.  
  422. public function parse_request( $request ) { 
  423. $to_save = array(); 
  424. if ( ! empty( $request['response'] ) && ! empty( $request['response']['code'] ) && 200 == $request['response']['code'] ) { 
  425. $dates = $this->get_date_range(); 
  426. $to_save['start-date'] = $dates['start']; 
  427. $to_save['end-date'] = $dates['end']; 
  428. $to_save['total'] = isset( $request['body']['totalsForAllResults'] ) ? $request['body']['totalsForAllResults'] : 0 ; 
  429. $to_save['data'] = isset( $request['body']['rows'] ) ? $request['body']['rows'] : array(); 
  430. } else { 
  431. monsterinsights_update_option( 'cron_failed', true ); 
  432. return $to_save; 
  433.  
  434. private function get_countries_array( $countries ) { 
  435. $js_countries = array(  
  436. "AF" => 0,  
  437. "AL" => 0,  
  438. "DZ" => 0,  
  439. "AO" => 0,  
  440. "AG" => 0,  
  441. "AR" => 0,  
  442. "AM" => 0,  
  443. "AU" => 0,  
  444. "AT" => 0,  
  445. "AZ" => 0,  
  446. "BS" => 0,  
  447. "BH" => 0,  
  448. "BD" => 0,  
  449. "BB" => 0,  
  450. "BY" => 0,  
  451. "BE" => 0,  
  452. "BZ" => 0,  
  453. "BJ" => 0,  
  454. "BT" => 0,  
  455. "BO" => 0,  
  456. "BA" => 0,  
  457. "BW" => 0,  
  458. "BR" => 0,  
  459. "BN" => 0,  
  460. "BG" => 0,  
  461. "BF" => 0,  
  462. "BI" => 0,  
  463. "KH" => 0,  
  464. "CM" => 0,  
  465. "CA" => 0,  
  466. "CV" => 0,  
  467. "CF" => 0,  
  468. "TD" => 0,  
  469. "CL" => 0,  
  470. "CN" => 0,  
  471. "CO" => 0,  
  472. "KM" => 0,  
  473. "CD" => 0,  
  474. "CG" => 0,  
  475. "CR" => 0,  
  476. "CI" => 0,  
  477. "HR" => 0,  
  478. "CY" => 0,  
  479. "CZ" => 0,  
  480. "DK" => 0,  
  481. "DJ" => 0,  
  482. "DM" => 0,  
  483. "DO" => 0,  
  484. "EC" => 0,  
  485. "EG" => 0,  
  486. "SV" => 0,  
  487. "GQ" => 0,  
  488. "ER" => 0,  
  489. "EE" => 0,  
  490. "ET" => 0,  
  491. "FJ" => 0,  
  492. "FI" => 0,  
  493. "FR" => 0,  
  494. "GA" => 0,  
  495. "GL" => 0,  
  496. "GM" => 0,  
  497. "GE" => 0,  
  498. "DE" => 0,  
  499. "GH" => 0,  
  500. "GR" => 0,  
  501. "GD" => 0,  
  502. "GT" => 0,  
  503. "GN" => 0,  
  504. "GW" => 0,  
  505. "GY" => 0,  
  506. "HT" => 0,  
  507. "HN" => 0,  
  508. "HK" => 0,  
  509. "HU" => 0,  
  510. "IS" => 0,  
  511. "IN" => 0,  
  512. "ID" => 0,  
  513. "IR" => 0,  
  514. "IQ" => 0,  
  515. "IE" => 0,  
  516. "IL" => 0,  
  517. "IT" => 0,  
  518. "JM" => 0,  
  519. "JP" => 0,  
  520. "JO" => 0,  
  521. "KZ" => 0,  
  522. "KE" => 0,  
  523. "KI" => 0,  
  524. "KR" => 0,  
  525. "KW" => 0,  
  526. "KG" => 0,  
  527. "LA" => 0,  
  528. "LV" => 0,  
  529. "LB" => 0,  
  530. "LS" => 0,  
  531. "LR" => 0,  
  532. "LY" => 0,  
  533. "LT" => 0,  
  534. "LU" => 0,  
  535. "MK" => 0,  
  536. "MG" => 0,  
  537. "MW" => 0,  
  538. "MY" => 0,  
  539. "MV" => 0,  
  540. "ML" => 0,  
  541. "MT" => 0,  
  542. "MR" => 0,  
  543. "MU" => 0,  
  544. "MX" => 0,  
  545. "MD" => 0,  
  546. "MN" => 0,  
  547. "ME" => 0,  
  548. "MA" => 0,  
  549. "MZ" => 0,  
  550. "MM" => 0,  
  551. "NA" => 0,  
  552. "NP" => 0,  
  553. "NL" => 0,  
  554. "NZ" => 0,  
  555. "NI" => 0,  
  556. "NE" => 0,  
  557. "NG" => 0,  
  558. "NO" => 0,  
  559. "OM" => 0,  
  560. "PK" => 0,  
  561. "PA" => 0,  
  562. "PG" => 0,  
  563. "PY" => 0,  
  564. "PE" => 0,  
  565. "PH" => 0,  
  566. "PL" => 0,  
  567. "PT" => 0,  
  568. "QA" => 0,  
  569. "RO" => 0,  
  570. "RU" => 0,  
  571. "RW" => 0,  
  572. "WS" => 0,  
  573. "ST" => 0,  
  574. "SA" => 0,  
  575. "SN" => 0,  
  576. "RS" => 0,  
  577. "SC" => 0,  
  578. "SL" => 0,  
  579. "SG" => 0,  
  580. "SK" => 0,  
  581. "SI" => 0,  
  582. "SB" => 0,  
  583. "ZA" => 0,  
  584. "ES" => 0,  
  585. "LK" => 0,  
  586. "KN" => 0,  
  587. "LC" => 0,  
  588. "VC" => 0,  
  589. "SD" => 0,  
  590. "SR" => 0,  
  591. "SZ" => 0,  
  592. "SE" => 0,  
  593. "CH" => 0,  
  594. "SY" => 0,  
  595. "TW" => 0,  
  596. "TJ" => 0,  
  597. "TZ" => 0,  
  598. "TH" => 0,  
  599. "TL" => 0,  
  600. "TG" => 0,  
  601. "TO" => 0,  
  602. "TT" => 0,  
  603. "TN" => 0,  
  604. "TR" => 0,  
  605. "TM" => 0,  
  606. "UG" => 0,  
  607. "UA" => 0,  
  608. "AE" => 0,  
  609. "GB" => 0,  
  610. "US" => 0,  
  611. "UY" => 0,  
  612. "UZ" => 0,  
  613. "VU" => 0,  
  614. "VE" => 0,  
  615. "VN" => 0,  
  616. "YE" => 0,  
  617. "ZM" => 0,  
  618. "ZW" => 0,  
  619. "KP" => 0,  
  620. "CU" => 0,  
  621. "PR" => 0,  
  622. "FK" => 0,  
  623. "SO" => 0,  
  624. "SS" => 0,  
  625. "EH" => 0,  
  626. "XK" => 0,  
  627. "XS" => 0,  
  628. "NC" => 0,  
  629. "PS" => 0,  
  630. ); 
  631.  
  632. if ( empty( $countries ) || ! is_array( $countries ) || empty( $countries['data'] ) || empty( $countries['total']['ga:pageviews'] ) ) { 
  633. // continue 
  634. } else { 
  635. $list_of_countries = array_flip( monsterinsights_get_country_list() ); 
  636. foreach ( $countries['data'] as $countries_index => $countries_values ) { 
  637. $title = isset( $countries_values[0] ) ? esc_html( $countries_values[0] ) : false; 
  638. if ( ! $title ) { 
  639. continue; 
  640.  
  641. if ( isset( $js_countries[ $title ] ) ) { 
  642. $views = isset( $countries_values[1] ) ? absint( $countries_values[1] ) : 0; 
  643. $js_countries[ $title ] = $views; 
  644. } else if ( isset( $list_of_countries[ $title ] ) ) { 
  645. $views = isset( $countries_values[1] ) ? absint( $countries_values[1] ) : 0; 
  646. $js_countries[ $list_of_countries[ $title ] ] = $views;  
  647. } else { 
  648. continue; 
  649.  
  650. $to_return = "{" . PHP_EOL; 
  651. foreach ( $js_countries as $country => $value ) { 
  652. $to_return .= '"' . esc_js( $country ) . '": ' . esc_js( $value ) . ", " . PHP_EOL; 
  653. $to_return .= "}" . PHP_EOL; 
  654. return $to_return;