xml, google script c#

I try to register the script below, and show on default.aspx

the codes look right but don't work to me. Please help.
protected void Page_Load(object sender, EventArgs e)
{
	string googleScript = string.Empty;
	if (domainName.Contains("aaa.com"))
	{
	 googleScript = "<script async src='https://www.googletagmanager.com/gtag/js?id=UA-923423499-4'></script>" +
               "<script> window.dataLayer = window.dataLayer || []; " +
               " function gtag(){ dataLayer.push(arguments); } " +
               " gtag('js', new Date()); " +
               " gtag('config', 'UA-923423499-4'); " +
               "</script>";
	}
	if (googleScript != "")
        {
                Page.ClientScript.RegisterStartupScript(this.GetType(), googleScript,"",true);
        }
}

//my goal is to add <script></script> on aspx webpage

Open in new window

LVL 1
ITsolutionWizardAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
try:

string googleScript = string.Empty;
            if (domainName.Contains("aaa.com"))
            {
                googleScript = "<script async src='https://www.googletagmanager.com/gtag/js?id=UA-923423499-4'></script>" +
                          "<script> function yourGoogleScript() { window.dataLayer = window.dataLayer || []; " +
                          " function gtag(){ dataLayer.push(arguments); } " +
                          " gtag('js', new Date()); " +
                          " gtag('config', 'UA-923423499-4'); } " +
                          "yourGoogleScript(); </script>";
            }
            if (googleScript != "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", googleScript, false);
            }

Open in new window

0
 
Ryan ChongCommented:
have you tested it out? it worked for me.
0
 
Ryan ChongCommented:
as proposed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.