确实是个烦人的问题。恐怕没有一个那么 容易 修复的。但是您可以使用
cron。
首先,请记住,Google不太可能因自己的工具(如Google
Analytics(分析))而对您进行惩罚。但是,如前所述,可以使用进行修复
cron,这基本上意味着您可以在本地加载Javascript并提取更新的脚本。
这个怎么做:
首先,您需要下载正在运行的脚本。我将以Google Analytics(分析)为例(这似乎是人们抱怨最多的问题脚本,但是您可以将其复制到任何外部脚本中)。
在您的代码中查找并找到脚本的名称,在本例中为:
google-analytics.com/ga.js。将此URL弹出到Web浏览器中,它将显示源代码。只需将其复制并另存为
ga.js。
在我的情况下,将此新创建的Javascript文件保存到您的网络服务器上:
- JS - ga.js
接下来,您将需要更新正在调用脚本的页面上的代码,只需更改正在调用Javascript文件的目录即可。在本例中,我们将再次更改此行:
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';至
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.yoursite.com/js/ga.js';至此,您的站点现在将从 本地 站点运行脚本!但是,这意味着脚本将永远不会更新。除非您每周重新运行此简短过程。那取决于你..但我对此实在太懒了。
这是CRON发挥作用的地方:
几乎每一个托管服务都会有一个选项供您设置
cron作业。在Hostinger上,它位于您的主机面板上,在GoDaddy上,您可以在“内容”选项下找到它。
将以下脚本放入您的脚本
cron,您所需要做的就是更改变量的绝对路径
$localfile。该脚本的作用是从Google提取文件的更新脚本
ga.js。您可以设置运行该过程的频率范围。从每小时一次到每月一次甚至更长时间不等。
如果您还针对Google
Analytics(分析)以外的外部文件执行此操作,则还需要更改变量
$remoteFile。所以,
$remoteFile是的URL外部Javascript文件和变量
$localFile,你就会把路到新的本地存储的文件,就这么简单!
<?// script to update local version of Google analytics script// Remote file to download$remoteFile = 'http://www.google-analytics.com/ga.js';$localfile = 'ENTER YOUR ABSOLUTE PATH TO THE FILE HERE';//For Cpanel it will be /home/USERNAME/public_html/ga.js// Connection time out$connTimeout = 10;$url = parse_url($remoteFile);$host = $url['host'];$path = isset($url['path']) ? $url['path'] : '/';if (isset($url['query'])) { $path .= '?' . $url['query'];}$port = isset($url['port']) ? $url['port'] : '80';$fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout );if(!$fp){ // On connection failure return the cached file (if it exist) if(file_exists($localfile)){ readfile($localfile); }} else { // Send the header information $header = "GET $path HTTP/1.0rn"; $header .= "Host: $hostrn"; $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6rn"; $header .= "Accept: */*rn"; $header .= "Accept-Language: en-us,en;q=0.5rn"; $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7rn"; $header .= "Keep-Alive: 300rn"; $header .= "Connection: keep-alivern"; $header .= "Referer: http://$hostrnrn"; fputs($fp, $header); $response = ''; // Get the response from the remote server while($line = fread($fp, 4096)){ $response .= $line; } // Close the connection fclose( $fp ); // Remove the headers $pos = strpos($response, "rnrn"); $response = substr($response, $pos + 4); // Return the processed response echo $response; // Save the response to the local file if(!file_exists($localfile)){ // Try to create the file, if doesn't exist fopen($localfile, 'w'); } if(is_writable($localfile)) { if($fp = fopen($localfile, 'w')){ fwrite($fp, $response); fclose($fp); } }}?>就是这样,它应该可以解决您利用Leverage Browser Caching第三方脚本遇到的所有问题。
资料来源:http : //diywpblog.com/leverage-browser-cache-optimize-google-
analytics/
注意:
实际上,这些文件不会对您的实际页面速度产生很大影响。但是我能理解您对Google惩罚您的担心。但是,如果你有一个,只会出现 大
运行这些外部脚本量。正如我之前所说,与Google相关的任何事情都不会对您不利。



