Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify a URL in the manual extension #1479

Merged
merged 5 commits into from Nov 23, 2021

Conversation

EmilePerron
Copy link
Contributor

Changes

This PR gives users the possibility of specifying a custom tracking location for their pages via a new u option in the manual script extension.

The main advantage of this addition is that it allows users to aggregate and/or redact URLs to improve the privacy and usefulness of their analytics. This helps to resolve the issues mentioned in #136.

Custom URLs can be specified either as a string, or a function which returns a string. Absolute URLs (complete with protocol and anchor) are the expected value, in order to maintain the same functionality as with the

Tests

  • This PR does not require tests

Changelog

  • Entry has been added to changelog

Documentation

  • Docs have been updated

Other notes and information

Usage examples:

<script defer data-domain="yourdomain.com" src="https://plausible.io/js/plausible.manual.js"></script>
<script>
// define the `plausible` function to manually trigger events
window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }

// Trigger pageview with a custom location
plausible('pageview'. {u: 'https://website.com/my/custom/location'})

// Alternatively, use a function
plausible('pageview'. {u: getMyLocation()})
</script>

The uglify-js dependency had to be updated to support the optional chaining operator
This added weight unnecessarily, as the users can simply call the function themselves before triggering the event.
@EmilePerron
Copy link
Contributor Author

Hey folks!

I went ahead and closed the previous PR for this feature since we moved to a completely different implementation.

For the documentation, should I simply mention this new feature in the section about the manual script extension, or should I create a new documentation page as I had started to do in plausible/docs#136 to explain why this might be useful and how to achieve it in more details?

I'll wait for your feedback before working on that part.

Cheers!

payload.u = options.u
}
{{else}}
payload.u = options?.u || location.href
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional chaining isn't supported in relatively recent (2019-2020) browsers: https://caniuse.com/?search=optional%20chaining

I think it's fine to just always use the standard if-else flow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

I went ahead and replaced it with the following ternary, which saves a few bytes over a regular if-else:
payload.u = options && options.u ? options.u : location.href

I also reverted the dependency update for uglify-js, as the old one better reflected your browser support stance.

Copy link
Contributor

@ukutaht ukutaht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I would like to remove the optional chaining but otherwise this is good to go

@metmarkosaric
Copy link
Contributor

For the documentation, should I simply mention this new feature in the section about the manual script extension, or should I create a new documentation page as I had started to do in plausible/docs#136 to explain why this might be useful and how to achieve it in more details?

thanks for your contribution @EmilePerron! in terms of docs, we can mention it on the script extensions page and then link from there to its own docs page. this is not the smallest feature to describe so best it has its own page

It was previously updated to support optional chaining; reverting it as a reminder that this is the current browser support level that is accepted by Plausible.
@EmilePerron
Copy link
Contributor Author

thanks for your contribution @EmilePerron! in terms of docs, we can mention it on the script extensions page and then link from there to its own docs page. this is not the smallest feature to describe so best it has its own page

Great! I'll go ahead and do just that then 👍

@ukutaht ukutaht merged commit fbd6e37 into plausible:master Nov 23, 2021
@ukutaht
Copy link
Contributor

ukutaht commented Nov 23, 2021

Thanks @EmilePerron. I'll let you know when this is live

@ukutaht
Copy link
Contributor

ukutaht commented Nov 23, 2021

@EmilePerron it's live

@ukutaht
Copy link
Contributor

ukutaht commented Nov 23, 2021

Could you test it and let me know how it works?

@EmilePerron
Copy link
Contributor Author

@ukutaht

Just tested it really briefly, looks like it works as intended.

Here's the snippet I used for all my pages :

<script defer data-domain="koalati.com" src="https://plausible.io/js/plausible.manual.js"></script>
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
<script>plausible("pageview");</script>

And to test it I ran the following in my browser's console:

plausible("pageview", { u: "https://koalati.webflow.io/test" })

The command in my console:

Screen Shot 2021-11-23 at 10 09 32 AM

The results on Plausible:

Screen Shot 2021-11-23 at 10 09 52 AM

@EmilePerron
Copy link
Contributor Author

I'll try and update my documentation MR tonight or tomorrow 👨🏻‍💻

@ukutaht
Copy link
Contributor

ukutaht commented Nov 23, 2021

@EmilePerron Thanks! Did it count double or did you manually trigger the second pageview?

@EmilePerron
Copy link
Contributor Author

@ukutaht I manually triggered the second pageview, which is why it shows up twice. So it feels to me like it works as intended - there's no duplicate count or anything :)

@ukutaht
Copy link
Contributor

ukutaht commented Nov 24, 2021

Wonderful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants