Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created May 28, 2012 18:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrigorik/2820414 to your computer and use it in GitHub Desktop.
Save igrigorik/2820414 to your computer and use it in GitHub Desktop.
pagespeed insights API ruby example
require 'net/https'
require 'json'
require 'uri'
#
# Use your project key to query the API
# 1) go to Google API Console: https://code.google.com/apis/console/
# 2) go to "Services" tab and enable "Page Speed Online API"
# 3) go to "API Key" tab and copy your "API Key"
#
KEY = 'YOUR_KEY'
strategy = 'desktop' # alternatively: 'mobile'
# hostname or page to run the test on
domain = 'http://www.igvita.com/'
url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=" + \
URI.encode(domain) + \
"&key=#{KEY}&strategy=#{strategy}"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
pss = JSON.parse(response.body)
jj pss
@igrigorik
Copy link
Author

@kkavit
Copy link

kkavit commented Oct 2, 2017

hi igrigorik. i have a question.
I want to use the page speed api in my project but it isnt a direct page. Its more of a home page->login(authenticate)->go to x page->click on this->go to y page. It is the y page that I need to get the performance statistics for. also its an internal test environment that I need to test this on. I am unable to understand how to specify all of this with a single "url" parameter. Please suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment