Binding server connect variable inside Javascript

thank you very much George.

Script tags and Style tags are not processed by App Connect. App Connect replaces the content when the DOM is ready, the javascript in the Script tag is then already executed.

then there is no way to are these two variabile to the script ?

Depending on where the data is coming from, lets say it comes from a serverconnect component. You want to create a javascript function on your site first like

function setUser() {
  Tawk_API.setAttributes({
    'name'  : dmx.app.data.serverconnect.data.visitor_name,
    'email' : dmx.app.data.serverconnect.data.email
  }, function(error){});
}

All data you access in expressions can also be accessed in JavaScript under dmx.app.data, so if your expression was {{serverconnect.data.visitor_name}} then you can access it as dmx.app.data.serverconnect.data.visitor_name in JavaScript.

You have to wait until App Connect has the data loaded, so on the serverconnect component listen to the load or success event and to trigger the function like

<dmx-serverconnect id="serverconnect" url="myaction.php" onsuccess="setUser()"></dmx-serverconnect>

I hope I didn’t make a mistake in my code, it is late here :slight_smile:

7 Likes

Yes it is late here too… thank you very much you are very kind !

You can also parse expressions directly using dmx.parse("serverconnect.data.visitor_name"), that is perhaps easier then accessing the data directly under dmx.app.data. You can then also use the formatters in the expressions.

3 Likes

I have to put it on the name piace inside the script ?

function setUser() {
  Tawk_API.setAttributes({
    'name'  : dmx.parse('serverconnect.data.visitor_name'),
    'email' : dmx.parse('serverconnect.data.email')
  }, function(error){ console.error(error); });
}

I’ve used the setAttribute of the Tawk API since I think the data will be available after the Tawk API finished loading, see documentation at https://www.tawk.to/javascript-api/. There could be a racing condition between the ajax call of the Server Connect and the Tawk API loading, if the Server Connect loaded before the Tawk API then the setAttribute will probably not work.

Did @patrick 's recomendation work?

Hi @patrick and @t11

First of all a big thank to Patrick for this extra support.

About the question of t11: have to say that this things it is too complex for my knowledge !
I made some try but for sure I did something not right. The fact is that I have not experience at all with javascript to manage something like that.

Now I m studing what Patrick wrote and the tawk.to api… let’s see if I can do something. In case I will keep you updated!!

Following with great interest as I have chat integration on a want list from a customer

1 Like

this sort of thing is what we need more please.
can you guys give more examples on app connect API. in the documentation

HI patrick,

is their any plans to allow variable binding to work within the script tags of the html?

Kind regards,

johnny hajjar

Hey @johnny.hajjar,

can you provide a specific example of what would you like to achieve?

Hi Teodor,

thank you for your quick response.

i’m using template monster to render large data sets into a pivot table.
https://jsfiddle.net/flexmonster/pz431qp5/

i need to be able to dynamically pass the json object , or jsonURL etc

var jsonData = {{jsonDS1.data}}

----original script------
var jsonData = [
{
“Color” : “green”,
“Country” : “Canada”,
“State” : “Ontario”,
“City” : “Toronto”,
“Price” : 174,
“Quantity” : 22
},
{
“Color” : “red”,
“Country” : “USA”,
“State” : “California”,
“City” : “Los Angeles”,
“Price” : 166,
“Quantity” : 19
}
];

hi Teodor,

was my use case explanation sufficient? :grinning:,

I really hope this is possible :slight_smile:

It is not possible to use the expressions like that in script and style tags. The expressions are evaluated after the DOM is loaded, the script and style tags are then already parsed by the browser. There are also no plans on making this possible in the future.

You can use data from the App Connect like var jsonData = dmx.parse('sonDS1.data'). You should call it after the data is loaded, it does not update like on the rest of the page. Best way is to create a javascript function with your code and do the parsing in there. Call the function on the load event of the server connect component.

Still trying to get somewhere with Tawk

Adding basic tawk is simple enough, just paste the JS in the body
However @updates question still remains problematic
I tried using Tawk_API.visitor = {} and spotted in the documentation that a secure hash is required based on the email address via the PHP function hash_hmac().
This led me to looking into sessions to see if i could access the email address that way
I have a security restrict names basr_security
within the server action i have a query (named query1) which returns fields FirstNames, LastNames, email and Applicant ID.
Everything OK so far
I then added the PHP line in the page var_dump($_SESSION);
This returned:
array(2) {
[“basr_securityId”]=>
int(16)
[“userdata”]=>
array(1) {
[0]=>
array(4) {
[“FirstNames”]=>
string(5) “Brian”
[“LastNames”]=>
string(7) “English”
[“email”]=>
string(22) "admin@hyperbytes.co.uk"
[“Applicant_ID”]=>
int(16)
}
}
}

This indicated the data returned by the query is also available as session variables?
However I have tried every variation to try and access those session variables and failed miserably
Anyone any ideas how to access the email session variable in PHP?
I have tried various variations on (many more tried,. not all listed
$_SESSION[“userdata.email”]
$_SESSION[“userdata[0].email”]
$_SESSION[“userdata.[0].email”]
$_SESSION[“basr_securityId.userdata.email”]
$_SESSION[“basr_security.userdata.email”]

Typical error message:
myaccount.php",“line”:47,“message”:“Undefined index: basr_securityId.userdata.[0].email”,“trace”:"#0 C:\bookastudentroom.com\public_html\users\myaccount.php(47): exception_error_handler(8, ‘Undefined index…’, ‘C:\\bookastudent…’, 47, Array)\n#1 {main}"}

Hi I solved for the moment setting up Tawkt.to chat starting with a form where people insert Name and email to start to chat (it is one of the default configurations).

Patrick gave me some suggestions but my knowledge of Javascript it is not so deep.

An extension would be nice :wink:

Seeing the php dump I think the session variable you want is $_SESSION["userdata"][0]["email"].

So in your script it would then become

<script type=“text/javascript”>
  var Tawk_API=Tawk_API||{};
  Tawk_API.visitor = {
    name : '<?php echo $_SESSION["userdata"][0]["FirstNames"] ?>',
    email : '<?php echo $_SESSION["userdata"][0]["email"] ?>'
  };
  var Tawk_LoadStart=new Date();
  // rest of the tawk.to widget code 
</script>

App Connect expressions in script blocks will not work, but PHP can be used inside the script blocks.