Google Analytics Injection with JSIL [SOLVED]

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
ebrooks
Posts: 3
Joined: Thu Nov 12, 2015 7:19 pm

Google Analytics Injection with JSIL [SOLVED]

Postby ebrooks » Tue Jan 05, 2016 11:29 am

Can someone post a snippet of how to inject Google Analytics from the C# side into the index.html without having to modify it each time? My guess is to use JSIL.Verbatim.Expression(...) but I'm not sure of what that looks like from the Silverlight app side...thanks!

ebrooks
Posts: 3
Joined: Thu Nov 12, 2015 7:19 pm

Re: Google Analytics Injection with JSIL

Postby ebrooks » Fri Jan 08, 2016 9:30 am

I think I solved this by doing the following. It seems to have injected the script just fine ;)

Code: Select all

private void EmbedGoogleAnalytics()
        {

            if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript)
            {
               JSIL.Verbatim.Expression("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" +
                                         "(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o)," +
                                         "m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)" +
                                         "})(window,document,'script','//www.google-analytics.com/analytics.js','ga');" +
                                         "  ga('create', 'UA-XXXXXXXXX-1', 'auto');" +
                                         "  ga('send', 'pageview');");
            }
            else
            {
                MessageBox.Show("Cannot run script injections when running inside simulator.");
            }

        }

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Google Analytics Injection with JSIL [SOLVED]

Postby JS-Support @Userware » Wed Jan 13, 2016 10:31 am

Well done ebrooks!

Thanks a lot for sharing your code.


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 37 guests

 

 

cron