Skip to content

Commit

Permalink
feat: make server configurable (#11)
Browse files Browse the repository at this point in the history
Add's `server` as a configuration option for using self-hosted Plausible instances.

Co-authored-by: Andrew Mason <[email protected]>
  • Loading branch information
whysthatso and andrewmcodes authored Sep 22, 2022
1 parent 888bdf7 commit 5c5cd29
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 24 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ or add manually in `Gemfile`:

```ruby
group :bridgetown_plugins do
gem "bridgetown-plausible", "~> 1.0.2"
gem "bridgetown-plausible", "~> 1.1.0"
end
```

Expand All @@ -74,6 +74,14 @@ plausible:
# Type: String
# Required: true
domain: example.com
# Your Plausible instance domain.
# Only set this if you are self-hosting Plausible on your own domain.
# Requires https.
#
# Type: String
# Required: false
# Default: "plausible.io"
server: selfhosted-plausible.com
```
## Usage
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
2 changes: 1 addition & 1 deletion bridgetown-plausible.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
spec.authors = ["Andrew Mason"]
spec.email = ["[email protected]"]
spec.summary = "Plausible Analytics Plugin for Bridgetown"
spec.description = "A Liquid tag to add Plausible analytics to your site."
spec.description = "Provides a Liquid Tag and ERB Helper to add Plausible analytics to your Bridgetown site."
spec.homepage = "https://github.com/bt-rb/#{spec.name}"
spec.license = "MIT"
spec.metadata = {
Expand Down
23 changes: 17 additions & 6 deletions bridgetown.automation.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
say_status :plausible, "Installing the bridgetown-plausible plugin..."

domain_name = ask("What's your Plausible domain?")

server_name = ask("If you are self-hosting Plausible, what's your instance domain? Leave blank if not self-hosting to default to plausible.io")
add_bridgetown_plugin "bridgetown-plausible"

append_to_file "bridgetown.config.yml" do
<<~YAML
if server_name == ""
append_to_file "bridgetown.config.yml" do
<<~YAML
plausible:
domain: #{domain_name}
YAML
end
else
append_to_file "bridgetown.config.yml" do
<<~YAML
plausible:
domain: #{domain_name}
YAML
plausible:
domain: #{domain_name}
server: #{server_name}
YAML
end
end

say_status :plausible, "All set! Double-check the plausible block in your config file and review docs at"
Expand Down
2 changes: 1 addition & 1 deletion demo/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
#
# Happy Bridgetowning!

gem "bridgetown", "~> 0.19.2"
gem "bridgetown", "~> 1.1.0"
group :bridgetown_plugins do
gem "bridgetown-plausible", path: "../"
end
10 changes: 9 additions & 1 deletion demo/bridgetown.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ plausible:
#
# Type: String
# Required: true
domain: example.com
domain: tracked-site.com
# Your Plausible instance domain.
# Only set this if you are self-hosting Plausible on your own domain.
# Requires https.
#
# Type: String
# Required: false
# Default: "plausible.io"
server: selfhosted-plausible.com
1 change: 0 additions & 1 deletion demo/plugins/site_builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class SiteBuilder < Bridgetown::Builder
# write builders which subclass SiteBuilder in plugins/builder
end

9 changes: 5 additions & 4 deletions lib/bridgetown-plausible/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ def build

def render
domain = options.dig(:domain)&.strip
server = options.dig(:server)&.strip || "plausible.io"

tag = if domain
markup_for_domain(domain)
markup_for_snippet(domain, server)
else
Bridgetown.logger.warn "Plausible", "Domain not configured."
markup_for_domain("NOT CONFIGURED")
markup_for_snippet("NOT CONFIGURED", server)
end

return wrap_with_comment(tag) unless Bridgetown.environment.production?

tag
end

def markup_for_domain(domain)
"<script async defer data-domain=\"#{domain}\" src=\"https://plausible.io/js/plausible.js\"></script>"
def markup_for_snippet(domain, server)
"<script async defer data-domain=\"#{domain}\" src=\"https://#{server}/js/plausible.js\"></script>"
end

def wrap_with_comment(tag)
Expand Down
52 changes: 44 additions & 8 deletions spec/bridgetown-plausible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@
end

context "when the rendering engine is liquid" do
let(:contents) { File.read(dest_dir("liquid.html")) }
let(:contents) { File.read(dest_dir("liquid/index.html")) }

context "when the domain is configured" do
let(:overrides) { {"plausible" => {"domain" => "example.com"}} }
let(:overrides) { {"plausible" => {"domain" => "tracked-site.com"}} }
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
<script async defer data-domain="tracked-site.com" src="https://plausible.io/js/plausible.js"></script>
HTML
end
end

context "when server && domain are configured" do
let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} }
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
<script async defer data-domain="example.com" src="https://plausible.io/js/plausible.js"></script>
<script async defer data-domain="tracked-site.com" src="https://selfhosted-plausible.com/js/plausible.js"></script>
HTML
end
end
Expand All @@ -42,13 +51,22 @@
end

context "when the rendering engine is erb" do
let(:contents) { File.read(dest_dir("erb.html")) }
let(:contents) { File.read(dest_dir("erb/index.html")) }

context "when the domain is configured" do
let(:overrides) { {"plausible" => {"domain" => "example.com"}} }
let(:overrides) { {"plausible" => {"domain" => "tracked-site.com"}} }
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
<script async defer data-domain="tracked-site.com" src="https://plausible.io/js/plausible.js"></script>
HTML
end
end

context "when server && domain are configured" do
let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} }
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
<script async defer data-domain="example.com" src="https://plausible.io/js/plausible.js"></script>
<script async defer data-domain="tracked-site.com" src="https://selfhosted-plausible.com/js/plausible.js"></script>
HTML
end
end
Expand All @@ -70,7 +88,7 @@
end

context "when the rendering engine is liquid" do
let(:contents) { File.read(dest_dir("liquid.html")) }
let(:contents) { File.read(dest_dir("liquid/index.html")) }

context "when the domain is configured" do
let(:overrides) { {"plausible" => {"domain" => "example.com"}} }
Expand All @@ -81,6 +99,15 @@
end
end

context "when server && domain are configured" do
let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} }
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
<!-- <script async defer data-domain="tracked-site.com" src="https://selfhosted-plausible.com/js/plausible.js"></script> -->
HTML
end
end

context "when the domain is not configured" do
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
Expand All @@ -91,7 +118,7 @@
end

context "when the rendering engine is erb" do
let(:contents) { File.read(dest_dir("erb.html")) }
let(:contents) { File.read(dest_dir("erb/index.html")) }

context "when the domain is configured" do
let(:overrides) { {"plausible" => {"domain" => "example.com"}} }
Expand All @@ -102,6 +129,15 @@
end
end

context "when server && domain are configured" do
let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} }
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
<!-- <script async defer data-domain="tracked-site.com" src="https://selfhosted-plausible.com/js/plausible.js"></script> -->
HTML
end
end

context "when the domain is not configured" do
it "outputs the correct HTML" do
expect(contents).to match <<~HTML
Expand Down

0 comments on commit 5c5cd29

Please sign in to comment.