feat(segment): bring back segment and block specific user agents#5944
Open
fabianrbz wants to merge 1 commit into
Open
feat(segment): bring back segment and block specific user agents#5944fabianrbz wants to merge 1 commit into
fabianrbz wants to merge 1 commit into
Conversation
✅ Deploy Preview for kongdeveloper ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Contributor
There was a problem hiding this comment.
Pull request overview
Reintroduces Segment analytics on the default layout while adding a UA-based bot filter, with the blocked UA list managed via site data during the Jekyll build.
Changes:
- Load a
blocked_user_agentsYAML file intosite.dataduring site generation. - Add Segment snippet back to
default.html, skipping zh-CN locales and blocking known bot/user-agent patterns. - Introduce a YAML config file containing exact and substring UA patterns to exclude from tracking.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| app/_plugins/generators/site_data.rb | Loads blocked UA config into site.data during site generation. |
| app/_layouts/default.html | Re-adds Segment snippet with locale gating and UA bot filtering. |
| app/_config/blocked_user_agents.yml | Defines exact/substr UA patterns to block from Segment tracking. |
Comment on lines
+52
to
+54
| def load_blocked_user_agents(site) | ||
| site.data['blocked_user_agents'] = YAML.load_file(File.expand_path('app/_config/blocked_user_agents.yml')) | ||
| end |
Comment on lines
+88
to
+89
| {% if jekyll.environment == "production" and page.url != '/search/' and page.url != '/support/' %} | ||
| <!-- Disable Segment on /search/ for now --> |
| if (!_isBot) { | ||
| !function(){var i="analytics",analytics=window[i]=window[i]||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","screen","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware","register"];analytics.factory=function(e){return function(){if(window[i].initialized)return window[i][e].apply(window[i],arguments);var n=Array.prototype.slice.call(arguments);if(["track","screen","alias","group","page","identify"].indexOf(e)>-1){var c=document.querySelector("link[rel='canonical']");n.push({__t:"bpc",c:c&&c.getAttribute("href")||void 0,p:location.pathname,u:location.href,s:location.search,t:document.title,r:document.referrer})}n.unshift(e);analytics.push(n);return analytics}};for(var n=0;n<analytics.methods.length;n++){var key=analytics.methods[n];analytics[key]=analytics.factory(key)}analytics.load=function(key,n){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.setAttribute("data-global-segment-analytics-key",i);t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r);analytics._loadOptions=n};analytics._writeKey="SMHOU8AoTR3Ekuuu4A9MkIOv6fmGfbkq";;analytics.SNIPPET_VERSION="5.2.0"; | ||
| analytics.load("SMHOU8AoTR3Ekuuu4A9MkIOv6fmGfbkq"); | ||
| analytics.page({{category}}, '{{page.title | liquify}}', {}, getSegmentConsentContext()); |
| var _botUaExact = new Set({{ site.data.blocked_user_agents.exact | jsonify }}); | ||
| var _botUaContains = {{ site.data.blocked_user_agents.substrings | jsonify }}; | ||
| var _ua = navigator.userAgent; | ||
| var _isBot = _botUaExact.has(_ua) || _botUaContains.some(function(p) { return _ua.toLowerCase().indexOf(p) !== -1; }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add segment and block specific user agents.
Preview Links
Checklist
descriptionentry in frontmatter.