Page 1 of 1

Google Analytics Injection with JSIL [SOLVED]

Posted: Tue Jan 05, 2016 11:29 am
by ebrooks
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!

Re: Google Analytics Injection with JSIL

Posted: Fri Jan 08, 2016 9:30 am
by ebrooks
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.");
            }

        }

Re: Google Analytics Injection with JSIL [SOLVED]

Posted: Wed Jan 13, 2016 10:31 am
by JS-Support @Userware
Well done ebrooks!

Thanks a lot for sharing your code.