Using Discourse variables to personalize embedded Typeform

In @sam’s example of an embedded Typeform, the url for the Typeform is:

https://basvanleeuwen1.typeform.com/to/NzdRpx?typeform-embed=embed-widget

That’s great, but I would like to personalize my Typeforms using hidden fields. If the Discourse username could be added to the url, then I could use it in the form. So when I visit, the url would be:

https://basvanleeuwen1.typeform.com/to/NzdRpx?typeform-embed=embed-widget&username=markschmucker

Is there a way to do that? Is it a candidate for a plugin?

Note you need a paid Typeform account for this to work.

Sounds a bit too fancy to be in the core onebox, the good news is that you will probably not need a plugin here a theme component can do the trick.

2 Likes

Thanks Sam. So something like this?

<script type="text/discourse-plugin" version="0.8">
  $( document ).ready(function() {
    const user = api.getCurrentUser(),
      username = user.username;

    $('h1.hello-world').html('Hello there '+username+"!")
  });
</script>

Except find and replace the url instead of saying Hello?

Yeah this is not the approach I would take, what you are looking for is decorate cooked in the plugin API.

2 Likes

Here is my (somewhat clumsy) solution, in case it helps someone. I added this code under Admin > Customize > Themes > Edit CSS/HTML > /head:

    <script type="text/discourse-plugin" version="0.8">
        api.decorateCooked($elem => {
            var i, j;
            var selector = 'https://example.typeform.com';
            // seems like this should work but it doesn't:
            // var typeforms = $elem[0].querySelectorAll('src^=[' + selector + ']');
            var ps = $elem[0].querySelectorAll('p');
            for (i=0; i<ps.length; i++) {
                p = ps[i];
                var iframes = p.querySelectorAll('iframe');
                for (j=0; j<iframes.length; j++) {
                    var iframe = iframes[j];
                    if (iframe.src.startsWith(selector)) {
                        var oldsrc = iframe.getAttribute('src');
                        var username = api.getCurrentUser()['username'];
                        var newsrc = oldsrc + '?username=' + username;
                        iframe.setAttribute('src', newsrc);
                    }
                }
            }
        });
    </script>

Then add a Hidden Field “username” in your Typeform. The username can be used in your form, and will also be sent to the destination (integrations or webhook) when the user hits Submit.

When you get the Typeform link to embed in Discourse, it will be something like:

https://example.typeform.com/to/dr8oAa?username=xxxxx

Remove the ?username=xxxxx before embedding; the js above will add it back, with the proper Discourse username.

1 Like

Anyone else coming across this later, I have an updated version of @mark78’s awesome post:

Add this code under Admin > Customize > Themes > Edit CSS/HTML > HEAD

:bangbang: If you are on enterprise, you’ll have to create an empty theme component to add this.

<script type="text/discourse-plugin" version="0.8.25">
    api.decorateCooked(($elem) => {
        const iframes = $elem[0].querySelectorAll('iframe[src^="https://form.typeform.com"]');
        const userName = api.getCurrentUser()?.username;
        
        if(!userName){ return; }
        
        for (let i = 0; i < iframes.length; i++) {
            iframes[i].setAttribute('src', `${iframes[i].getAttribute('src')}&username=${userName}`);
        }
    });
</script>
  • Add a hidden field of ‘username’ in your Typeform
  • Embed your Typeform on your post by simply sharing the link. Do not use the Typeform embed feature. You will also need to allow the Typeform URL https://forms.typeform.com as an embed in your admin settings.