Fix current user issue and add tracking features (#1)

* Add cross subdomain tracking

* Add cross subdomain tracking

* Enable crossdomain linking

* Move crossdomain code to a better place

* Remove subdomain from site title

* Fix all subdomains parameter

* Be more specific in subdomain tracking description

* Add user ID tracking

* Handle whitespace around user id field

* Exclude some groups from tracking

* Add exmaple groups to exclude from tracking

* Fix whitespace handling in user ID tracking

* Fix null reference on anonymous users
This commit is contained in:
Ryan Boder 2019-09-19 13:04:24 -04:00 committed by Robin Ward
parent bf3b766146
commit bbd0dcb82d
2 changed files with 33 additions and 2 deletions

View File

@ -1,5 +1,15 @@
<script type="text/discourse-plugin" version="0.2"> <script type="text/discourse-plugin" version="0.2">
api.onPageChange((url, title) => { api.onPageChange((url, title) => {
const currentUser = api.getCurrentUser();
if (settings.exclude_groups.trim() && currentUser) {
const excludedGroups = settings.exclude_groups.split(",").map(g => g.trim());
const currentUserGroups = currentUser.groups.map(g => g.name);
if (excludedGroups.filter(g => currentUserGroups.includes(g)).length) {
return;
}
}
window._paq = window._paq || []; window._paq = window._paq || [];
window._paq_loaded = window._paq_loaded || false; window._paq_loaded = window._paq_loaded || false;
@ -12,8 +22,17 @@
_paq_loaded = true; _paq_loaded = true;
} }
const currentUser = api._currentUser; const userIdField = settings.user_id_tracking.trim();
_paq.push(['setCustomVariable', 1, 'Anonymous', !currentUser, 'visit']); if (userIdField && currentUser && currentUser[userIdField]) {
_paq.push(['setUserId', currentUser[userIdField]]);
}
if (settings.subdomain_tracking) {
const allDomains = "*" + document.domain.substring(document.domain.indexOf("."));
_paq.push(["setCookieDomain", allDomains]);
_paq.push(["setDomains", allDomains]);
}
_paq.push(["setCustomUrl", url]); _paq.push(["setCustomUrl", url]);
_paq.push(["setDocumentTitle", title]); _paq.push(["setDocumentTitle", title]);
_paq.push(["trackPageView"]); _paq.push(["trackPageView"]);

View File

@ -5,3 +5,15 @@ host_url:
website_id: website_id:
type: string type: string
default: '' default: ''
exclude_groups:
type: string
default: ''
description: Comma separated list of groups to exclude from tracking such as admins, staff.
user_id_tracking:
type: string
default: ''
description: An indentifying field name on the current user object such as id, username or external_id.
subdomain_tracking:
type: bool
default: false
description: Track visitors across main domain & subdomains, assuming discourse is on a subdomain.