Sending response to an AJAX call

Gold Contributor
Gold Contributor

Hi everybody,

 

one of our partners asked me to make a function that sets a data cookie everytime our website is called with a set of specific GET-parameters. I have used the "Persist Data Value" extension to do so. So TIQ checks for the parameters and sets the cookie - no matter what HTML-page is called.

 

Now it turns out that the partner is using JSONP through his AJAX call. This means that the response must not be HTML. They need a page with the content type application/javascript as a response and headers should include Set-Cookie: [partner name].

 

I was first thinking of writing a javascript-file to be placed on the server - but then I could also write the code to set the cookie in this javascript. But I want all functions to be maintained in TIQ.

 

Does anybody have an idea on how to achieve this or can put me in the right direction?

 

Thanks in advance!!

 

Best,

 

Bernd

6 REPLIES 6

Sending response to an AJAX call

Tealium Employee

@sangmister

 

I'm not a JSONP expert, but based on the JSONP integrations I've done, it's basically a callback that passes in an object of data. You should be able to create a JS extension with the basic outline below to handle the data accordingly.

 

//check to see if partner cookies exists in utag_main with a valid value
if(typeof b["cp.partnerCookie"]=="undefined"||b["cp.partnerCookie"]=="undefined"){

   //declare function to handle partner data
   window.getPartnerData=function(o){
      try{
         if(o){
            //do something
         }
      }catch(e){
         utag.DB(e)
      }
   }

   //call out to partner to request their data then fire the callback function
   utag.ut.loader({
      id:"my_partner_data",
      src:"//partner.endpoint.com/i.json?......&callback=getPartnerData"
   });
}

 

I did find a stackoverflow article that seems to talk about handling HTML, but it's still something returned within the function so you should be able to use the above template to figure out how to handle the response.

https://stackoverflow.com/questions/8642949/jsonp-return-html

 

Apologies, but withouth having documentation available or more insight into the call this is the best advise I can give at this time. Please let me know if this is or is not helpful, or if you have anything questions.

 

Cheers,

-Dan

Sending response to an AJAX call

Gold Contributor
Gold Contributor

@dan_george,

 

thanks a lot for your quick reply!

 

I think your suggestion assumes that I want to receive data from our partner via JSON. But it's rather the other way around. The partner is sending a request like this:

 

http://www.mainova.de/anypage.html?parameter1=x&parameter2=y&etc

 

Currently I just compile the parameter values in a variable and use the "Persist Data" Extension to store the values in a cookie. That's fine so far.

 

Problem is that the partner calls the link above through an AJAX call and expects a JSON answer. I don't know how to acheive this. Of course I could simply write a JavaScript that collects the parameter values, stores the cookie and sends a JSON answer - but then I would not need Tealium IQ. And I would like to use TIQ to keep this maintainable.

 

Thanks and best regards,

 

Bernd

Sending response to an AJAX call

Tealium Employee

@sangmister

 

Thanks for the clarification. Yea, this is a tricky one. My suggestion is as follows:

 

First same background, remember that each tag configuration you make creates a file on the Tealium mCDN. For example, any tag you add and publish will be of format:

http://tags.tiqcdn.com/utag/<account>/<profile>/<environment>/utag.<Tag-UID>.js

 

Therefore, you can use Tealium to create and host your own JS file by adding a Tealium Custom Container, removing all the template code, and housing your code in place of the template code. 

 

I'm making a few general assumptions here; I'm not exactly the most familar with the AJAX/JSON/JSONP requirements or limitations that a browser might have, but this is the only example I can think of where Tealium can host the file for you and you can manage the code within Tealium.

 

Thoughts? @adrian_browning might have some good insight. He's a hacker at heart and a Tealium hotshot as well.

 

Cheers,

-Dan

Sending response to an AJAX call

Gold Contributor
Gold Contributor

Thanks a lot, @dan_george!

 

I like the idea of using utag.xx.js and I've played around with that for a while. Unfortuately, this doesn't work as well - if I set a cookie in this js, it would come from tags.tiqcdn.com and not from our domain.

 

So I've tried to call my existing version via an AJAX call within utag.xx.js. This didn't work as well, since no cookie is set if the script is called via AJAX.

 

It seems I have to write a simple JS and place it on the server - which makes me a bit sad. ;)

 

Does anybody have some more ideas on this?

Sending response to an AJAX call

Tealium Employee

@dan_george Thanks for the compliment ;)

 

@sangmister

 

With regards to using a utag.x.js you will need to make sure that this tag has a load rule that can never fire. Else it will mess up the utag.js because if you remove the LOAD() call in the template, utag.js will be waiting forever for the file to say it has loaded.

The way to get around this is to set the custom script source to be the tags URL. This will cause utag to wrap a callback on the tag and everything will be happy.

 

With regards to setting cookies, from a utag.x.js script, we can set either first-party cookies (your site) or on the tags.tealiumiq.com domain. Am I right in assuming that you want to be able to set a cookie on another 3rd party domain? If this is correct you would need that domain to respond with a set-cookie header.

 

Adrian

Ask me anything Tealium related or TypeScript/JavaScript, or NodeJS.
Please remember to mark solutions as accepted for future searchers.

Sending response to an AJAX call

Gold Contributor
Gold Contributor

Thanks, @adrian_browning!

Yes, I want to set a cookie from our domain on another (3rd party) domain. They sent requirements where they mention a "JSONP" solution. After a bit of fiddling around, I've decided to pass this issue to our Java developers. The JSONP solution would require manipulation of the http headers, and I guess that's not possible to achieve with JavaScript. I think we need a webservice here.

But thanks a lot for your help and your ideas - I think I can use the idea of using utag.x.js in some other contexts!

Regards,

Bernd

Public