How can I automatically target different forms between dev and production environments? | Community
Skip to main content
Answered

How can I automatically target different forms between dev and production environments?

  • December 15, 2022
  • 5 replies
  • 190 views

Ted Cohn
Forum|alt.badge.img

I use WordPress and copy my dev to prd environment, which copies the Typeform id from dev to prd too. But I’d like to have two Typeforms each for dev and prd. Is there some clever way to configure the Typeform to use the desired form id depending on the domain or environment? I’m thinking it could be harder where I’d have to write script code to do this on my page.

Best answer by Ted Cohn

Ok, I’ve discovered I can simply fetch the current web page’s location using document.location.host and insert the result into the hidden source field. Then my back end can differentiate where the form is filled out and take appropriate action.

Here’s my current embedded HTML script that works for me. It inserts both the currently logged in Wordpress user id and the source. 

<!-- Present Typeform with hidden user_id and source fields -->
<div id="tf"
     data-tf-widget="<your form id here>"
     data-tf-iframe-props="title=<Your form title>"
     data-tf-medium="snippet"
     data-tf-hidden="user_id=xxxxx,source=xxxxx"
     style="width:100%;height:600px;">
</div>
<script src="//embed.typeform.com/next/embed.js"></script>
<script>
    const user_id = document.cookie.match(/portal_user=([^;]+);/)[1]
    const host = document.location.host
    const div = document.querySelector('#tf')
    div.dataset.tfHidden = "user_id=" + user_id + ",source=" + host
</script>

 

View original

5 replies

Liz
Community Team
Forum|alt.badge.img+5
  • Tech Community Advocate
  • 14884 replies
  • December 16, 2022

Hi @Ted Cohn Happy Friday! We don’t have this feature in Typeform, so you would need to write a code on your website that can do this. 

@mathio may possibly have some examples of this, but I believe this is very specific to your website and how you’ve built it. 


Ted Cohn
Forum|alt.badge.img
  • Author
  • Socializer
  • 7 replies
  • December 16, 2022

@Liz Thanks. Are there any code samples ideally for Wordpress that simply insert the current site’s host or URL where the form is presented? I have two websites, a staging site and production site each with different URLs. If I can plug into the hidden source field, my backend can then differentiate where the user is entering data. 


john.desborough
Forum|alt.badge.img+6
  • Certified Partner & Champion
  • 5267 replies
  • December 16, 2022

@Ted Cohn - this may not be exactly what you want to achieve, but for a couple of clients, when i created the form for the dev environment, we customized the link to a naming convention that was formname-dev and when we duplicated the form for the prod site, we changed the name to -prod … 

your web developers should be able to ‘dynamically’ create-push a variable string to replace the -dev/-prod portion of the url string, based on which instance is being viewed by the user.. 

just a thought.. 

 

des


Ted Cohn
Forum|alt.badge.img
  • Author
  • Socializer
  • 7 replies
  • Answer
  • December 16, 2022

Ok, I’ve discovered I can simply fetch the current web page’s location using document.location.host and insert the result into the hidden source field. Then my back end can differentiate where the form is filled out and take appropriate action.

Here’s my current embedded HTML script that works for me. It inserts both the currently logged in Wordpress user id and the source. 

<!-- Present Typeform with hidden user_id and source fields -->
<div id="tf"
     data-tf-widget="<your form id here>"
     data-tf-iframe-props="title=<Your form title>"
     data-tf-medium="snippet"
     data-tf-hidden="user_id=xxxxx,source=xxxxx"
     style="width:100%;height:600px;">
</div>
<script src="//embed.typeform.com/next/embed.js"></script>
<script>
    const user_id = document.cookie.match(/portal_user=([^;]+);/)[1]
    const host = document.location.host
    const div = document.querySelector('#tf')
    div.dataset.tfHidden = "user_id=" + user_id + ",source=" + host
</script>

 


Liz
Community Team
Forum|alt.badge.img+5
  • Tech Community Advocate
  • 14884 replies
  • December 16, 2022

Ah, thanks for sharing this, @Ted Cohn !!