Access Discourse.User.currentProp email via javascript?

(Allen - Watchman Monitoring) #1

Is there any chance the javascript API can be expanded to include the user’s email address in the javascript API?

That way our chat & other tools could be updated via


just the same way we can get username & name

(Jeff Atwood) #2

Unlikely as email is viewed as a privileged setting…, maybe if the request is made with an admin user API key?

(Allen - Watchman Monitoring) #3

ok… I may not be asking the right thing then, or don’t understand…

I think I’m asking for the logged in user’s email address to be returned in the page he or she is receiving, so that a javascript I add in the Admin page can access it, and send it on to a third party tool (like our LiveChat plugin, so our users can request help without having to re-enter their email address)

If I’m correct, then I don’t see how the email address could be put into the hands of someone who didn’t already have it.

(Allen - Watchman Monitoring) #4

The script I’m adding is working fine for “Name” when there is one.

I’ve got it loaded here:


<script type="text/javascript">
__lc.visitor = { name: Discourse.User.currentProp("name"), email: Discourse.User.currentProp("email") };

(email comes back as undefined in that example… and I just wish I could surface the user’s email address there too)

(Kane York) #5

Ah. I think you need to make a separate request to fetch “your own” email.

(Allen - Watchman Monitoring) #6

I don’t follow… The email address we want to send the third party integration is the email of the logged in visitor.

The data is included in the page he or she receives, and the included javascript can scrape it up.

Example uses:
Olark JavaScript API
$zopim.livechat - Javascript API
LiveChat – Chat Window API (JS) - adding custom variables

The code gets embedded in discourse via the admin customization page, and it’s currently working for .name and .username … I’m asking that it work with email too

(Kane York) #7

I’m saying that the email is not included in the page you receive with Discourse. You have to find some endpoint for the JS to make a request to that gets the current user’s email address.

(Allen - Watchman Monitoring) #8

Yes, here I was asking for it to be included, citing that I don’t see any security risk, given that we would think the logged-in user would know it themselves.


(Jesse Perry) #9

I’m currently trying to get this to work by pulling in the user profile via JSON and parse out the email client-side via RegEx or something.


Getting the current username from Discourse.User.currentProp(‘username’)

(Jesse Perry) #10

Here you go! Returns currentuseremail as variable (replace DISCOURSEDOMAIN):

Discourse.PageTracker.current().on('change', function(url, title) {
    var currentusername = Discourse.User.currentProp('username');
    var fulllinktojson = "DISCOURSEDOMAIN/users/" + currentusername + ".json";
    $.getJSON(fulllinktojson, function(json){
        var currentuseremail = String(;

Update: @watchmanmonitor slight update above, just added “String()” to return as string (didn’t realize it wasn’t)

How to get current via javascript?
(Travis) #11

Sorry for reviving this topic. Can this be used to insert the insert_email_variable, in this block?

fbq('init', 'xxx', {
em: 'insert_email_variable,'
fbq('track', 'PageView');

(Daniel Lopes) #12

I had the same problem when trying to connect with my Discourse app. I solved this by keeping the code in a plugin because I like to have everything versioned but you could just add this via the admin as well:

import { withPluginApi } from 'discourse/lib/plugin-api';

function initializeSegment(api) {
  api.onPageChange((url, title) => {
    !function(){var||[];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","once","off","on"];analytics.factory=function(t){return function(){var;e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+""+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";

    const user = Discourse.User.current();

    if (user !== undefined) {
      Discourse.User.findByUsername(user.username).then((u) => {
        analytics.identify(, {

export default {
  name: 'kyc-segment',
  initialize() {
    if (Discourse.Environment !== "development") {
      withPluginApi('0.8.7', initializeSegment);