Google Analytics不会注册出站链接点击次数

时间:2016-03-18 08:36:17

标签: javascript google-analytics

这是我在头部中的Google Analytics代码。

@Test
public void test3() {
    try {
        // create file
        File        input  = folder.newFile( "input.txt" );
        File        output = folder.newFile( "output.txt" );

        PrintWriter write  = new PrintWriter( input );
        write.println( "32 nickels" );
        write.println( " 1" );
        write.println( "   nickel 42" );
        write.println( "quarters 1 penny" );
        write.println( "1 quarter 23 pennies 16" );
        write.println( "" );
        write.println( "dimes 1 dime 1 dime 1 dime 1 dime" );
        write.close();

        // invoke program
        CookieJar.cashingIn( input, output );

        // verify file results
        assertTrue  ( "Output file does not exist", output.exists() );
        Scanner scan     = new Scanner( output );
        String  expected = "You have $14.64 in the jar";
        assertTrue  ( "Unexpected end of file: expected \"%s\"" + expected, scan.hasNext() );
        String  actual   = scan.nextLine();
        assertEquals( "Incorrect result", expected, actual );
        assertFalse ( "File contains more data than expected", scan.hasNext() );
        scan.close();
    } 
    catch (IOException e) {
        fail( "No exception should be thrown" );
    }
}

我认为这是新的Google Universal Analytics代码。在标题中添加Google Analytics代码后,我添加了这样的链接。

<script type="text/javascript">
  (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');
</script>

这已经过了一个月,但Google Analytics并没有注册出站链接点击次数。我是否必须在某处添加任何其他代码?

1 个答案:

答案 0 :(得分:2)

在你的JS中返回false; 将在ga()函数调用之后进行。代码将是:

    <a href="http://domain.com" rel="external nofollow" target="_blank" onclick="javascript:window.open('http://domain.com');ga('send','event','Outgoing Links','http://domain.com','Top content link'); return false;">Some text</a>

此外,在发送链接时使用这样的ga:

    ga('send', 'event', decodeURIComponent('Outgoing Links'), decodeURIComponent('http://domain.com'), decodeURIComponent('Top content link')) ;