SHARE
TWEET

Untitled

a guest Nov 6th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. if (!function_exists('curl_reset'))
  3. {
  4.     function curl_reset(&$ch)
  5.     {
  6.         $ch = curl_init();
  7.     }
  8. }
  9. //error_reporting(0);
  10. $site = explode("=",$_SERVER['QUERY_STRING']);
  11. $date = date("Y-n-j");
  12. $hari = date("j",strtotime($date));
  13. $bulan = date("n",strtotime($date));
  14. $tahun = date("Y",strtotime($date));
  15.  
  16. $cDate = strtotime(date('Y-m-d H:i:s'));
  17. $state = apc_fetch('date');
  18. $flag=0;
  19. if(!$state):
  20.     $nDate = strtotime(date('Y-m-d 24:00:00'));
  21.     apc_store('date', $nDate);
  22.     $state = apc_fetch('date');
  23. endif;
  24. if($state < $cDate):
  25.     apc_delete('date');
  26.     $nDate = strtotime(date('Y-m-d 24:00:00'));
  27.     apc_store('date', $nDate);
  28.     $flag=1;
  29. endif;
  30. require_once __DIR__ . '/vendor/autoload.php';
  31.  
  32. $analytics = initializeAnalytics();
  33. $response = getReport($analytics,$site[1]);
  34. printResults($response,$site[1],$bulan,$hari,$tahun);
  35. merge($flag);
  36.  
  37. function initializeAnalytics()
  38. {
  39.   $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';
  40.   $client = new Google_Client();
  41.   $client->setApplicationName("Hello Analytics Reporting");
  42.   $client->setAuthConfig($KEY_FILE_LOCATION);
  43.   $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  44.   $analytics = new Google_Service_AnalyticsReporting($client);
  45.  
  46.   return $analytics;
  47. }
  48.  
  49.  
  50. function getReport($analytics,$website) {
  51.  
  52.   $VIEW_ID = "90636734";
  53.  
  54.   // Create the DateRange object.
  55.   $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  56.   $dateRange->setStartDate("today");
  57.   $dateRange->setEndDate("today");
  58.  
  59.   // Create the Metrics object.
  60.   $pageview = new Google_Service_AnalyticsReporting_Metric();
  61.   $pageview->setExpression("ga:pageviews");
  62.   $pageview->setAlias("Pageviews");
  63.  
  64.  
  65.   // Create the dimension object.
  66.   $id = new Google_Service_AnalyticsReporting_Dimension();
  67.   $id->setName("ga:dimension13");
  68.  
  69.   $month = new Google_Service_AnalyticsReporting_Dimension();
  70.   $month->setName("ga:dimension16");
  71.  
  72.   $year = new Google_Service_AnalyticsReporting_Dimension();
  73.   $year->setName("ga:dimension17");
  74.    
  75.   $day = new Google_Service_AnalyticsReporting_Dimension();
  76.   $day->setName("ga:day");
  77.  
  78.   $pagePath = new Google_Service_AnalyticsReporting_Dimension();
  79.   $pagePath->setName("ga:pagepath");
  80.  
  81.   $dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
  82.   $dimensionFilter->setDimensionName("ga:dimension5");
  83.   $dimensionFilter->setOperator("EXACT");
  84.   $dimensionFilter->setExpressions(array("$website"));
  85.  
  86.   $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
  87.   $dimensionFilterClause->setFilters(array($dimensionFilter));
  88.  
  89.  
  90.   $ordering = new Google_Service_AnalyticsReporting_OrderBy();
  91.   $ordering->setFieldName("ga:pageviews");
  92.   $ordering->setOrderType("VALUE");  
  93.   $ordering->setSortOrder("DESCENDING");
  94.  
  95.  
  96.   // Create the ReportRequest object.
  97.   $request = new Google_Service_AnalyticsReporting_ReportRequest();
  98.   $request->setViewId($VIEW_ID);
  99.   $request->setDateRanges($dateRange);
  100.   $request->setDimensions(array($id,$year,$month,$day));
  101.   $request->setMetrics(array($pageview));
  102.   $request->setDimensionFilterClauses(array($dimensionFilterClause));
  103.   $request->setOrderBys($ordering);
  104.  
  105.   $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  106.   $body->setReportRequests( array( $request) );
  107.   return $analytics->reports->batchGet( $body );
  108. }
  109.  
  110. function connect_db($tribun,$id,$hit){
  111.             //$servername = "daerah-staging-2.cttdtdmogujb.ap-southeast-1.rds.amazonaws.com";
  112.                 $servername = "dbtdaerah.cttdtdmogujb.ap-southeast-1.rds.amazonaws.com";
  113.                 $username = "tdaerah-appuse";
  114.                 $password = "t12bun7d43rah-7ri3unD1g17alonlin3-app7d43rah-u53r2015";   
  115.             $conn = new mysqli($servername, $username, $password);
  116.             //  if ($conn->connect_error) {
  117.             //      die("Connection failed: " . $conn->connect_error);
  118.             //   }
  119.             //  if($tribun == "aceh") $db_select = mysqli_select_db($conn, "aceh2");
  120.             //  elseif($tribun == "jambi") $db_select = mysqli_select_db($conn, "jambi2"); 
  121.             //  else
  122.                 $db_select = mysqli_select_db($conn, "kalteng");
  123.                     if (!$db_select) {
  124.                         die("Database selection failed: " . mysqli_error($conn))."<br>";
  125.                         $mssg = "Database selection failed";
  126.                     }
  127.                     else {
  128.                             $sql = "SELECT h.hit,a.publish_date FROM hit2 h inner join articles a ON h.article_id = a.id where h.article_id ='$id'";
  129.                             $result = $conn->query($sql);
  130.                             $current = $result->fetch_array();
  131.                                 if ($result->num_rows > 0) {
  132.                                       $date = date("Y-n-j");
  133.                                       $publish_date = date("Y-n-j",strtotime($current[publish_date]));
  134.                                       if($publish_date != $date):
  135.                                         $hit = $hit + $current[hit];
  136.                                         $sql1 = "UPDATE hit2 SET view ='$hit' WHERE article_id='$id'";
  137.                                         $conn->query($sql1);
  138.                                       else:
  139.                                         $sql1 = "UPDATE hit2 SET hit ='$hit' WHERE article_id='$id'";
  140.                                         $conn->query($sql1);
  141.                                       endif;
  142.                                 } else {
  143.                                     $sql2 = " INSERT INTO hit2 (article_id,hit) VALUES ('$id','$hit')" ;
  144.                                     $conn->query($sql2);
  145.                                 }
  146.                             }
  147.                 $conn->close();
  148. }
  149. function merge($flag){
  150.         //$servername = "daerah-staging-2.cttdtdmogujb.ap-southeast-1.rds.amazonaws.com";
  151.         $servername = "dbtdaerah.cttdtdmogujb.ap-southeast-1.rds.amazonaws.com";
  152.         $username = "tdaerah-appuse";
  153.         $password = "t12bun7d43rah-7ri3unD1g17alonlin3-app7d43rah-u53r2015";   
  154.         $conn = new mysqli($servername, $username, $password);
  155.             //  if ($conn->connect_error) {
  156.             //      die("Connection failed: " . $conn->connect_error);
  157.             //   }
  158.             //  if($tribun == "aceh") $db_select = mysqli_select_db($conn, "aceh2");
  159.             //  elseif($tribun == "jambi") $db_select = mysqli_select_db($conn, "jambi2"); 
  160.             //  else
  161.             $db_select = mysqli_select_db($conn, "kalteng");
  162.             if (!$db_select) {
  163.                 die("Database selection failed: " . mysqli_error($conn))."<br>";
  164.                 $mssg = "Database selection failed";
  165.                 }
  166.             else {
  167.                     if($flag == 1):
  168.                         $cal = "SELECT * from hit2 where view != '0'";
  169.                         $resultCal = $conn->query($cal);
  170.                         while ($row=mysqli_fetch_row($resultCal))
  171.                             {   $sum = $row[1]+$row[2];
  172.                                 $update = "UPDATE hit2 SET hit ='$sum',view = '0' WHERE article_id='$row[0]'";
  173.                                 $updateCal = $conn->query($update);
  174.                             }
  175.                     endif;
  176.                
  177.                   }
  178.             $conn->close();
  179. }
  180.  
  181. function printResults($reports,$site,$a,$b,$c) {
  182.   for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  183.     $report = $reports[ $reportIndex ];
  184.     $header = $report->getColumnHeader();
  185.     $dimensionHeaders = $header->getDimensions();
  186.     $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  187.     $rows = $report->getData()->getRows();
  188.     for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  189.       $row = $rows[ $rowIndex ];
  190.       $dimensions = $row->getDimensions();
  191.       $metrics = $row->getMetrics();
  192.       for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  193.        //print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "<br>");
  194.             if($dimensionHeaders[$i] == "ga:dimension13"):
  195.                 $id = $dimensions[$i];
  196.             elseif($dimensionHeaders[$i] == "ga:day"):
  197.                 $day = $dimensions[$i];
  198.             elseif($dimensionHeaders[$i] == "ga:dimension17"):
  199.                 $year = $dimensions[$i];
  200.             else:
  201.                 $month = $dimensions[$i];
  202.             endif;
  203.       }
  204.  
  205.       for ($j = 0; $j < count($metrics); $j++) {
  206.         $values = $metrics[$j]->getValues();
  207.         for ($k = 0; $k < count($values); $k++) {
  208.           $entry = $metricHeaders[$k];
  209.             //print($entry->getName() . ": " . $values[$k] );
  210.             $hit = $values[$k];
  211.         }
  212.       }
  213.      
  214.         if($month == $a && $day == $b && $year == $c):
  215.             connect_db($site,$id,$hit,$flag);
  216.             echo "site =>".$site." id Article => ".$id."  Pageviews =>".$hit."  Month =>".$month."<br>";
  217.         endif;
  218.    
  219.     }
  220.   }
  221. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top