Skip to content

Commit

Permalink
Added TAWKTO_EXCLUDE_SUPERUSERS config
Browse files Browse the repository at this point in the history
  • Loading branch information
CleitonDeLima committed Jan 26, 2022
1 parent 0ba3dbf commit 5fdc3e9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ or
{% tawkto_script id_site='tawktosideit' api_key='tawktoapikey'%}
```

(Optional) If you want to hide the chat for superuser (default is `True`):

```python
TAWKTO_EXCLUDE_SUPERUSERS = True
```


(Optional) Set custom metadata regarding this chat/visitor:

- `is_secure` must be `True`.
Expand Down
32 changes: 32 additions & 0 deletions tawkto/templates/tawkto/templatetags/_script.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--Start of Tawk.to Script-->
{{ tawkto_data|json_script:"tawkto_data" }}
<script type="text/javascript">
const TawkData = JSON.parse(document.getElementById("tawkto_data").innerText);
window.Tawk_API = window.Tawk_API || {};
window.Tawk_LoadStart = new Date();

if (TawkData.visitor.name || TawkData.visitor.email) {
window.Tawk_API.visitor = TawkData.visitor;
}

if (TawkData.is_secure && TawkData.visitor.email && TawkData.extra_attributes) {
window.Tawk_API.onLoad = function(){
window.Tawk_API.setAttributes(TawkData.extra_attributes, function(error){
if (error) {
console.log("setAttributes API error", error);
}
});
};
}

(function () {
var s1 = document.createElement("script");
var s0 = document.getElementsByTagName("script")[0];
s1.async = true;
s1.src = TawkData.url;
s1.charset = "UTF-8";
s1.setAttribute("crossorigin", "*");
s0.parentNode.insertBefore(s1, s0);
})();
</script>
<!--End of Tawk.to Script-->
35 changes: 3 additions & 32 deletions tawkto/templates/tawkto/templatetags/tawkto_script.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
<!--Start of Tawk.to Script-->
{{ tawkto_data|json_script:"tawkto_data" }}
<script type="text/javascript">
const TawkData = JSON.parse(document.getElementById("tawkto_data").innerText);
window.Tawk_API = window.Tawk_API || {};
window.Tawk_LoadStart = new Date();

if (TawkData.visitor.name || TawkData.visitor.email) {
window.Tawk_API.visitor = TawkData.visitor;
}

if (TawkData.is_secure && TawkData.visitor.email && TawkData.extra_attributes) {
window.Tawk_API.onLoad = function(){
window.Tawk_API.setAttributes(TawkData.extra_attributes, function(error){
if (error) {
console.log("setAttributes API error", error);
}
});
};
}

(function () {
var s1 = document.createElement("script");
var s0 = document.getElementsByTagName("script")[0];
s1.async = true;
s1.src = TawkData.url;
s1.charset = "UTF-8";
s1.setAttribute("crossorigin", "*");
s0.parentNode.insertBefore(s1, s0);
})();
</script>
<!--End of Tawk.to Script-->
{% if not exclude_superusers %}
{% include 'tawkto/templatetags/_script.html' with tawkto_data=tawkto_data only %}
{% endif %}
6 changes: 5 additions & 1 deletion tawkto/templatetags/tawkto_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def tawkto_script(**kwargs):
default_api_key = getattr(settings, "TAWKTO_API_KEY", None)
default_widget_id = getattr(settings, "TAWKTO_WIDGET_ID", "default")
default_is_secure = getattr(settings, "TAWKTO_IS_SECURE", False)
exclude_superusers = getattr(settings, "TAWKTO_EXCLUDE_SUPERUSERS", True)

user_email = kwargs.get("user_email", "")
user_name = kwargs.get("user_name", "")
Expand Down Expand Up @@ -43,4 +44,7 @@ def tawkto_script(**kwargs):
).hexdigest()
data["visitor"]["hash"] = hash_hmac

return {"tawkto_data": data}
return {
"tawkto_data": data,
"exclude_superusers": exclude_superusers,
}
1 change: 1 addition & 0 deletions test_project/test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@
TAWKTO_API_KEY = os.environ.get("TAWKTO_API_KEY", "")
TAWKTO_WIDGET_ID = os.environ.get("TAWKTO_WIDGET_ID", "")
TAWKTO_IS_SECURE = True
TAWKTO_EXCLUDE_SUPERUSERS = False
5 changes: 3 additions & 2 deletions test_project/test_project/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path

from app.views import test_view
from django.contrib import admin
from django.urls import path

urlpatterns = [
path("admin/", admin.site.urls),
path("", test_view),
]

0 comments on commit 5fdc3e9

Please sign in to comment.