Skip to content

Commit

Permalink
use primaryDomain to query CrUX API
Browse files Browse the repository at this point in the history
Most shops map custom domains and we should use that to query CrUX API,
since that's what real users hit! Closes #6.

Note: there is no REST API for this? =/
  • Loading branch information
igrigorik committed Aug 27, 2020
1 parent 1048f60 commit 8646408
Show file tree
Hide file tree
Showing 5 changed files with 90,210 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Optimizing quality of user experience is critical to the success of every storef

[![CWV for Shopify preview](storage/cwv-shopify-preview.jpg)](storage/cwv-shopify-preview.jpg)

Take it for a spin on your own **development** store: https://core-web-vitals-dashboard.herokuapp.com/login

Take it for a spin on your own **development** store: https://core-web-vitals-dashboard.herokuapp.com/login

### Development

Expand All @@ -24,6 +23,12 @@ Take it for a spin on your own **development** store: https://core-web-vitals-da
`SHOPORIGIN='https://www.allbirds.com' shopify serve`
1. Navigate to the tunnel URL and install the dev app on your Shopify store

### Updating GraphQL API version

1. Get token from `@current_shopify_session.domain`
1. Run the update via
`rake shopify_api:graphql:dump API_VERSION=XXXX-XX SHOP_DOMAIN=<DOMAIN> ACCESS_TOKEN=<TOKEN>`

### Contributing

Have suggestions on how to improve the report, or a feature you'd like to nominate? Head to [Issues](/issues) and start the conversation.
10 changes: 5 additions & 5 deletions app/controllers/competitors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

class CompetitorsController < AuthenticatedController
before_action do
@shop = Shop.find_by(shopify_domain: @current_shopify_session.domain)
@shop = Shop.find_by(shopify_domain: helpers.get_primary_shop_domain)
end

def show
render :json => @shop.competitors
render json: @shop.competitors
end

def create
@shop.competitors << Competitor.new(origin: params[:origin])
render :json => @shop.save
render json: @shop.save
end

def destroy
if (c = @shop.competitors.find_by(origin: params[:origin]))
render :json => c.destroy
render json: c.destroy
else
render :json => { :error => "not found" }
render json: {error: "not found"}
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
# Shopify will invalidate the granted keys.
@scopes = ShopifyAPI::AccessScope.find(:all)

@shop = Shop.find_by(shopify_domain: @current_shopify_session.domain)
@shop = Shop.find_by(shopify_domain: helpers.get_primary_shop_domain)
@competitors = @shop.competitors.pluck(:origin)
@competitors = DEFAULT_COMPETITORS if @competitors.empty?
end
Expand Down
18 changes: 18 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
module ApplicationHelper
SHOP_QUERY = <<-'GRAPHQL'
{
shop {
name,
primaryDomain {
id,
url
}
}
}
GRAPHQL

def get_primary_shop_domain
client = ShopifyAPI::GraphQL.client
result = client.query(client.parse(SHOP_QUERY))

URI.parse(result.data.shop.primary_domain.url).host
end
end
Loading

0 comments on commit 8646408

Please sign in to comment.