Search Unity

How would I define a <script> element with a url from Application.ExternalEval?

Discussion in 'Web' started by heroichippo1, Dec 10, 2015.

  1. heroichippo1

    heroichippo1

    Joined:
    Mar 30, 2015
    Posts:
    36
    Hi Guys,

    Our publisher is using a third party ad provider for running UA on facebook, and their analytic systems require a custom javascript "pixel" for tracking advertisement revenue and conversion. Their system requires a call like this.

    <script src="someurl.com?someparams></script> and that is how they accept the events.

    Since the actual loading of the script from the servers actually fires the events, and I need it to fire based off of IAP events dynamically in my game code, I need to be able to load the javascript from the src url they give me, but inside an Application.ExternalCall or Applicaiton.ExternalEval call in my game.

    Is there any way to do this?

    If i do this code, I get an error that the < is not expected symbol because its html, not javascript.

    Code (CSharp):
    1. string ftbPixel = @"
    2. <!-- JS script tag for product: MyGame triggering action: FTB-->
    3.  
    4. <script type=""text / javascript"" src=""someurl?ifcontext=&order_id=" + ThirdPartyID.Value + @"""></script>
    5.  
    6. < !--JS script tag pixel for product: My Game triggering action: FTB-- >
    7. ";
    8.                 Application.ExternalEval(ftbPixel);
    is there some way to do this with like, document.write() or something?
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    something like this should work (not tested, might need some changes):

    Code (csharp):
    1.  
    2. Application.ExternalEval("var script = document.createElement('script');script.src = 'http://my.url.com/';document.body.appendChild(script);");
    3.  
     
  3. heroichippo1

    heroichippo1

    Joined:
    Mar 30, 2015
    Posts:
    36
    Thanks jonas, that worked perfectly :)

    for those interested the exact code turned out to be this.

    Code (CSharp):
    1.     string purchasePixel = @"
    2.    var purchasePixel = document.createElement(""script"");
    3.    purchasePixel.type = ""text/javascript"";
    4.    purchasePixel.src = ""//myurl?order_id=" + ThirdPartyID.Value + @"&amount=" + result.Items[0].UnitPrice +     @"&currency=USD"";
    5.    document.getElementsByTagName(""body"")[0].appendChild(purchasePixel);
    6.    ";
    7.          
    8.     Application.ExternalEval(purchasePixel);