ÿþ/ /   C o p y r i g h t   2 0 1 1   G o o g l e   I n c .   A l l   R i g h t s   R e s e r v e d .  
  
 / * *  
   *   @ f i l e o v e r v i e w   A   s i m p l e   s c r i p t   t o   a u t o m a t i c a l l y   t r a c k   F a c e b o o k   a n d   T w i t t e r  
   *   b u t t o n s   u s i n g   G o o g l e   A n a l y t i c s   s o c i a l   t r a c k i n g   f e a t u r e .  
   *   @ a u t h o r   a p i . n i c k m @ g o o g l e . c o m   ( N i c k   M i h a i l o v s k i )  
   * /  
  
  
 / * *  
   *   N a m e s p a c e .  
   *   @ t y p e   { O b j e c t } .  
   * /  
 v a r   _ g a   =   _ g a   | |   { } ;  
  
  
 / * *  
   *   E n s u r e   g l o b a l   _ g a q   G o o g l e   A n a l y t i c s   q u e u e   h a s   b e e n   i n i t i a l i z e d .  
   *   @ t y p e   { A r r a y }  
   * /  
 v a r   _ g a q   =   _ g a q   | |   [ ] ;  
  
  
 / * *  
   *   H e l p e r   m e t h o d   t o   t r a c k   s o c i a l   f e a t u r e s .   T h i s   a s s u m e s   a l l   t h e   s o c i a l  
   *   s c r i p t s   /   a p i s   a r e   l o a d e d   s y n c h r o n o u s l y .   I f   t h e y   a r e   l o a d e d   a s y n c ,  
   *   y o u   m i g h t   n e e d   t o   a d d   t h e   n e x t w o r k   s p e c i f i c   t r a c k i n g   c a l l   t o   t h e  
   *   a   c a l l b a c k   o n c e   t h e   n e t w o r k ' s   s c r i p t   h a s   l o a d e d .  
   *   @ p a r a m   { s t r i n g }   o p t _ p a g e U r l   A n   o p t i o n a l   U R L   t o   a s s o c i a t e   t h e   s o c i a l  
   *           t r a c k i n g   w i t h   a   p a r t i c u l a r   p a g e .  
   *   @ p a r a m   { s t r i n g }   o p t _ t r a c k e r N a m e   A n   o p t i o n a l   n a m e   f o r   t h e   t r a c k e r   o b j e c t .  
   * /  
 _ g a . t r a c k S o c i a l   =   f u n c t i o n ( o p t _ p a g e U r l ,   o p t _ t r a c k e r N a m e )   {  
     _ g a . t r a c k F a c e b o o k ( o p t _ p a g e U r l ,   o p t _ t r a c k e r N a m e ) ;  
     _ g a . t r a c k T w i t t e r ( o p t _ p a g e U r l ,   o p t _ t r a c k e r N a m e ) ;  
 } ;  
  
  
 / * *  
   *   T r a c k s   F a c e b o o k   l i k e s ,   u n l i k e s   a n d   s e n d s   b y   s u s c r i b i n g   t o   t h e   F a c e b o o k  
   *   J S A P I   e v e n t   m o d e l .   N o t e :   T h i s   w i l l   n o t   t r a c k   f a c e b o o k   b u t t o n s   u s i n g   t h e  
   *   i F r a m e   m e t h o d .  
   *   @ p a r a m   { s t r i n g }   o p t _ p a g e U r l   A n   o p t i o n a l   U R L   t o   a s s o c i a t e   t h e   s o c i a l  
   *           t r a c k i n g   w i t h   a   p a r t i c u l a r   p a g e .  
   *   @ p a r a m   { s t r i n g }   o p t _ t r a c k e r N a m e   A n   o p t i o n a l   n a m e   f o r   t h e   t r a c k e r   o b j e c t .  
   * /  
 _ g a . t r a c k F a c e b o o k   =   f u n c t i o n ( o p t _ p a g e U r l ,   o p t _ t r a c k e r N a m e )   {  
     v a r   t r a c k e r N a m e   =   _ g a . b u i l d T r a c k e r N a m e _ ( o p t _ t r a c k e r N a m e ) ;  
     t r y   {  
         i f   ( F B   & &   F B . E v e n t   & &   F B . E v e n t . s u b s c r i b e )   {  
             F B . E v e n t . s u b s c r i b e ( ' e d g e . c r e a t e ' ,   f u n c t i o n ( t a r g e t U r l )   {  
                 _ g a q . p u s h ( [ t r a c k e r N a m e   +   ' _ t r a c k S o c i a l ' ,   ' f a c e b o o k ' ,   ' l i k e ' ,  
                         t a r g e t U r l ,   o p t _ p a g e U r l ] ) ;  
             } ) ;  
             F B . E v e n t . s u b s c r i b e ( ' e d g e . r e m o v e ' ,   f u n c t i o n ( t a r g e t U r l )   {  
                 _ g a q . p u s h ( [ t r a c k e r N a m e   +   ' _ t r a c k S o c i a l ' ,   ' f a c e b o o k ' ,   ' u n l i k e ' ,  
                         t a r g e t U r l ,   o p t _ p a g e U r l ] ) ;  
             } ) ;  
             F B . E v e n t . s u b s c r i b e ( ' m e s s a g e . s e n d ' ,   f u n c t i o n ( t a r g e t U r l )   {  
                 _ g a q . p u s h ( [ t r a c k e r N a m e   +   ' _ t r a c k S o c i a l ' ,   ' f a c e b o o k ' ,   ' s e n d ' ,  
                         t a r g e t U r l ,   o p t _ p a g e U r l ] ) ;  
             } ) ;  
         }  
     }   c a t c h   ( e )   { }  
 } ;  
  
  
 / * *  
   *   R e t u r n s   t h e   n o r m a l i z e d   t r a c k e r   n a m e   c o n f i g u r a t i o n   p a r a m e t e r .  
   *   @ p a r a m   { s t r i n g }   o p t _ t r a c k e r N a m e   A n   o p t i o n a l   n a m e   f o r   t h e   t r a c k e r   o b j e c t .  
   *   @ r e t u r n   { s t r i n g }   I f   o p t _ t r a c k e r N a m e   i s   s e t ,   t h e n   t h e   v a l u e   a p p e n d e d   w i t h  
   *           a   .   O t h e r w i s e   a n   e m p t y   s t r i n g .  
   *   @ p r i v a t e  
   * /  
 _ g a . b u i l d T r a c k e r N a m e _   =   f u n c t i o n ( o p t _ t r a c k e r N a m e )   {  
     r e t u r n   o p t _ t r a c k e r N a m e   ?   o p t _ t r a c k e r N a m e   +   ' . '   :   ' ' ;  
 } ;  
  
  
 / * *  
   *   T r a c k s   e v e r y t i m e   a   u s e r   c l i c k s   o n   a   t w e e t   b u t t o n   f r o m   T w i t t e r .  
   *   T h i s   s u b s c r i b e s   t o   t h e   T w i t t e r   J S   A P I   e v e n t   m e c h a n i s m   t o   l i s t e n   f o r  
   *   c l i c k s   c o m i n g   f r o m   t h i s   p a g e .   D e t a i l s   h e r e :  
   *   h t t p : / / d e v . t w i t t e r . c o m / p a g e s / i n t e n t s - e v e n t s # c l i c k  
   *   T h i s   m e t h o d   s h o u l d   b e   c a l l e d   o n c e   t h e   t w i t t e r   A P I   h a s   l o a d e d .  
   *   @ p a r a m   { s t r i n g }   o p t _ p a g e U r l   A n   o p t i o n a l   U R L   t o   a s s o c i a t e   t h e   s o c i a l  
   *           t r a c k i n g   w i t h   a   p a r t i c u l a r   p a g e .  
   *   @ p a r a m   { s t r i n g }   o p t _ t r a c k e r N a m e   A n   o p t i o n a l   n a m e   f o r   t h e   t r a c k e r   o b j e c t .  
   * /  
 _ g a . t r a c k T w i t t e r   =   f u n c t i o n ( o p t _ p a g e U r l ,   o p t _ t r a c k e r N a m e )   {  
     v a r   t r a c k e r N a m e   =   _ g a . b u i l d T r a c k e r N a m e _ ( o p t _ t r a c k e r N a m e ) ;  
     t r y   {  
         i f   ( t w t t r   & &   t w t t r . e v e n t s   & &   t w t t r . e v e n t s . b i n d )   {  
             t w t t r . e v e n t s . b i n d ( ' t w e e t ' ,   f u n c t i o n ( e v e n t )   {  
                 i f   ( e v e n t )   {  
                     v a r   t a r g e t U r l ;   / /   D e f a u l t   v a l u e   i s   u n d e f i n e d .  
                     i f   ( e v e n t . t a r g e t   & &   e v e n t . t a r g e t . n o d e N a m e   = =   ' I F R A M E ' )   {  
                         t a r g e t U r l   =   _ g a . e x t r a c t P a r a m F r o m U r i _ ( e v e n t . t a r g e t . s r c ,   ' u r l ' ) ;  
                     }  
                     _ g a q . p u s h ( [ t r a c k e r N a m e   +   ' _ t r a c k S o c i a l ' ,   ' t w i t t e r ' ,   ' t w e e t ' ,  
                         t a r g e t U r l ,   o p t _ p a g e U r l ] ) ;  
                 }  
             } ) ;  
         }  
     }   c a t c h   ( e )   { }  
 } ;  
  
  
 / * *  
   *   E x t r a c t s   a   q u e r y   p a r a m e t e r   v a l u e   f r o m   a   U R I .  
   *   @ p a r a m   { s t r i n g }   u r i   T h e   U R I   f r o m   w h i c h   t o   e x t r a c t   t h e   p a r a m e t e r .  
   *   @ p a r a m   { s t r i n g }   p a r a m N a m e   T h e   n a m e   o f   t h e   q u e r y   p a r a m a t e r   t o   e x t r a c t .  
   *   @ r e t u r n   { s t r i n g }   T h e   u n - e n c o d e d   v a l u e   o f   t h e   q u e r y   p a r a m a t e r .   u n d e r f i n e d  
   *           i f   t h e r e   i s   n o   U R I   p a r a m e t e r .  
   *   @ p r i v a t e  
   * /  
 _ g a . e x t r a c t P a r a m F r o m U r i _   =   f u n c t i o n ( u r i ,   p a r a m N a m e )   {  
     i f   ( ! u r i )   {  
         r e t u r n ;  
     }  
     v a r   u r i   =   u r i . s p l i t ( ' # ' ) [ 0 ] ;     / /   R e m o v e   a n c h o r .  
     v a r   p a r t s   =   u r i . s p l i t ( ' ? ' ) ;     / /   C h e c k   f o r   q u e r y   p a r a m s .  
     i f   ( p a r t s . l e n g t h   = =   1 )   {  
         r e t u r n ;  
     }  
     v a r   q u e r y   =   d e c o d e U R I ( p a r t s [ 1 ] ) ;  
  
     / /   F i n d   u r l   p a r a m .  
     p a r a m N a m e   + =   ' = ' ;  
     v a r   p a r a m s   =   q u e r y . s p l i t ( ' & ' ) ;  
     f o r   ( v a r   i   =   0 ,   p a r a m ;   p a r a m   =   p a r a m s [ i ] ;   + + i )   {  
         i f   ( p a r a m . i n d e x O f ( p a r a m N a m e )   = = =   0 )   {  
             r e t u r n   u n e s c a p e ( p a r a m . s p l i t ( ' = ' ) [ 1 ] ) ;  
         }  
     }  
     r e t u r n ;  
 } ; 
