cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
nikviz
Advocate I
Advocate I

Capture the client id from the webpage and send it via PVA

Hi

 

I have hosted my PVA on an existing webpage. After the conversation ends in PVA, data is pushed to Dynamics (using power automate). 


Webpage uses ga client id (Googla Analytics) to retrieve the client id. Is it possible to retrieve the client id from webpage and push it through PVA while user is engaging with the bot. We would then be pushing this id to the dynamics. 

 

I checked for any connectors that can do this and couldn't find any. 

Is there any way I can achieve this?
 

I tried the below

 

```

var gaclientid;

gtag('get', <clientid>, 'client_id', (client_id) => {
gaclientid = client_id;
var fieldList = document.getElementsByName("gaClientID");
fieldList.forEach(element => {
element.value = client_id;
});
});

const store = window.WebChat.createStore(
{},
({ dispatch }) => next => action => {
if (action.type === "DIRECT_LINE/CONNECT_FULFILLED") {
dispatch({
meta: {
method: "keyboard",
},
payload: {
activity: {
channelData: {
postBack: true,
},
//Web Chat will show the 'Greeting' System Topic message which has a trigger-phrase 'hello'
name: 'startConversation',
type: "event",

},
},
type: "DIRECT_LINE/POST_ACTIVITY",
});

store.dispatch({ type: 'WEB_CHAT/SET_SEND_BOX',
payload: { text: gaclientid ,
} });

}
return next(action);
}
);

fetch(theURL)
.then(response => response.json())
.then(conversationInfo => {
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({
token: conversationInfo.token,
}),

store: store,
styleOptions: styleOptions
},
document.getElementById('webchat')
);
})
.catch(err => console.error("An error occurred: " + err));

 

```

I was able to fetch the distinct id from the web browser and get it into "SEND BOX" as shown below

 

nikviz_0-1665071279501.png

 

However, I want this value to be stored in a variable and sent it via bot conversation without user seeing it. 

This would be the unique user ID, which I will later be storing in dataverse.

1 ACCEPTED SOLUTION

Accepted Solutions
nikviz
Advocate I
Advocate I

Solved this by doing the below

Step 1:
In the HTML script:

 

     var gaclientid;

            gtag('get', <clientid>, 'client_id', (client_id) => {
                gaclientid = client_id;
                var fieldList = document.getElementsByName("gaClientID");
                fieldList.forEach(element => {
                element.value = client_id;
                       });
                       
                    });


            // Add your BOT ID below
            var BOT_ID = "<bot_id>";


           

            const store = window.WebChat.createStore(
               {},
               ({ dispatch }) => next => action => {
                   if (action.type === "DIRECT_LINE/CONNECT_FULFILLED") {

                   
                      dispatch({
                          meta: {
                               method: "keyboard",
                           },
                           payload: {
                           
                               activity: {
                                     channelData: {
                                            postBack: true,
                                                                                                                             
                                     },
                                      //Web Chat will show the 'Greeting' System Topic message which has a trigger-phrase 'hello'
                                      name: "startConversation",
                                      type: "event",
                                      value: {
                                        "gaclientid": ""+ gaclientid + "",
                                         }
                                                                           
                                 },
                            },
                            type: "DIRECT_LINE/POST_ACTIVITY",
                     
                        });
                     
                 }
                 return next(action);
              }
           );
           fetch(theURL)
                .then(response => response.json())
                .then(conversationInfo => {
                    window.WebChat.renderWebChat(
                        {
                            directLine: window.WebChat.createDirectLine({
                                token: conversationInfo.token,
                            }),
                           // onTelemetry:
                            store: store,
                            styleOptions: styleOptions
                           
                        },
                        document.getElementById('webchat')
                    );
                })
                .catch(err => console.error("An error occurred: " + err));
 
Step 2: 
In PVA Create a Topic and set variable where external sources can set values.
The question doesnt matter here, it's just that we want the variable to be accessed externally
nikviz_0-1668611904893.png

 

Use it in the conversation flow however you want

nikviz_1-1668612023756.png

 

View solution in original post

1 REPLY 1
nikviz
Advocate I
Advocate I

Solved this by doing the below

Step 1:
In the HTML script:

 

     var gaclientid;

            gtag('get', <clientid>, 'client_id', (client_id) => {
                gaclientid = client_id;
                var fieldList = document.getElementsByName("gaClientID");
                fieldList.forEach(element => {
                element.value = client_id;
                       });
                       
                    });


            // Add your BOT ID below
            var BOT_ID = "<bot_id>";


           

            const store = window.WebChat.createStore(
               {},
               ({ dispatch }) => next => action => {
                   if (action.type === "DIRECT_LINE/CONNECT_FULFILLED") {

                   
                      dispatch({
                          meta: {
                               method: "keyboard",
                           },
                           payload: {
                           
                               activity: {
                                     channelData: {
                                            postBack: true,
                                                                                                                             
                                     },
                                      //Web Chat will show the 'Greeting' System Topic message which has a trigger-phrase 'hello'
                                      name: "startConversation",
                                      type: "event",
                                      value: {
                                        "gaclientid": ""+ gaclientid + "",
                                         }
                                                                           
                                 },
                            },
                            type: "DIRECT_LINE/POST_ACTIVITY",
                     
                        });
                     
                 }
                 return next(action);
              }
           );
           fetch(theURL)
                .then(response => response.json())
                .then(conversationInfo => {
                    window.WebChat.renderWebChat(
                        {
                            directLine: window.WebChat.createDirectLine({
                                token: conversationInfo.token,
                            }),
                           // onTelemetry:
                            store: store,
                            styleOptions: styleOptions
                           
                        },
                        document.getElementById('webchat')
                    );
                })
                .catch(err => console.error("An error occurred: " + err));
 
Step 2: 
In PVA Create a Topic and set variable where external sources can set values.
The question doesnt matter here, it's just that we want the variable to be accessed externally
nikviz_0-1668611904893.png

 

Use it in the conversation flow however you want

nikviz_1-1668612023756.png

 

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Top Solution Authors
Users online (1,414)