Use utm_source=blender to urls pointing to blender.org #107849

Closed
Dalai Felinto wants to merge 3 commits from dfelinto/blender:web-development into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
1 changed files with 39 additions and 12 deletions

View File

@ -1018,9 +1018,44 @@ class WM_OT_url_open(Operator):
description="URL to open",
)
@staticmethod
def _add_utm_param_to_url(url, utm_source):
import urllib
# Make sure we have a scheme otherwise we can't parse the url.
if not url.startswith(("http://", "https://")):
url = "https://" + url
# Parse the URL to get its domain and query parameters.
parsed_url = urllib.parse.urlparse(url)
domain = parsed_url.netloc
# Only add a utm source if it points to a blender.org domain.
if not (domain.endswith(".blender.org") or domain == "blender.org"):

if not domain.endswith(".blender.org"):

otherwise this would still add it to "https://ohno.totally.notblender.org" which is probably not what we want

`if not domain.endswith(".blender.org"):` otherwise this would still add it to "https://ohno.totally.notblender.org" which is probably not what we want

good point, I now check for .blender.org or if the entire domain is blender.org (in case someone forgets to use www.)

good point, I now check for .blender.org or if the entire domain is blender.org (in case someone forgets to use www.)
return url
# Parse the query parameters and add or update the utm_source parameter.
query_params = urllib.parse.parse_qs(parsed_url.query)
query_params["utm_source"] = utm_source
new_query = urllib.parse.urlencode(query_params, doseq=True)
# Create a new URL with the updated query parameters.
new_url_parts = list(parsed_url)
new_url_parts[4] = new_query
new_url = urllib.parse.urlunparse(new_url_parts)
return new_url
@staticmethod
def _get_utm_source():
version = bpy.app.version_string
formatted_version = version.replace(' ', '-').lower()
return f"blender-{formatted_version}"
def execute(self, _context):
import webbrowser
webbrowser.open(self.url)
complete_url = self._add_utm_param_to_url(self.url, self._get_utm_source())
webbrowser.open(complete_url)
return {'FINISHED'}
@ -1102,10 +1137,7 @@ class WM_OT_url_open_preset(Operator):
url = url(self, context)
break
import webbrowser
webbrowser.open(url)
return {'FINISHED'}
return bpy.ops.wm.url_open(url=url)
class WM_OT_path_open(Operator):
@ -1307,9 +1339,7 @@ class WM_OT_doc_view_manual(Operator):
)
return {'CANCELLED'}
else:
import webbrowser
webbrowser.open(url)
return {'FINISHED'}
return bpy.ops.wm.url_open(url=url)
class WM_OT_doc_view(Operator):
@ -1325,10 +1355,7 @@ class WM_OT_doc_view(Operator):
if url is None:
return {'CANCELLED'}
import webbrowser
webbrowser.open(url)
return {'FINISHED'}
return bpy.ops.wm.url_open(url=url)
rna_path = StringProperty(