Raj
Raj

Reputation: 31

Using Java, how to verify/validate Google Analytics Asynchronus Tracking Code(Analytics snippet ) is under the <head> tag section of a webpage source

Here is the example of the Analytics snippet to be verified that it is a part of head section of a web page source code

<script type="text/javascript">
    var _gag = _gag || [];
   _gag.push(['_setAccount','UA-XXXX-X']);  
   _gag.push(['_trackPageView']);   

    setTimeout(function(){
        var ga = document.createElement('script');ga.type ="text/javascript"; ga.sync = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http//www') + '.google-analytics.com/ga.js';
    var ga = document.getElementsByTagName9'script')[0];
        s.parentnode.insertBefore(ga, s);
    });             

Upvotes: 0

Views: 368

Answers (1)

Diana R
Diana R

Reputation: 1174

If you use selenium webdriver than you can search all script tags in the and iterate over the list to find the matching script name page by :

List<WebElement> scriptList = webDriver.findElements(By.tagName("script"))
boolean scriptFound = false;
for(WebElement item : scriptList){
    scriptFound = item.getAttribute("src").contains("google-analytics.com/ga.js");
}
 if(!scriptFound) {}//fail the test

Upvotes: 1

Related Questions