CefRegisterExtension(). How to insert google analytics code?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

CefRegisterExtension(). How to insert google analytics code?

Postby CantStop0708 » Thu Jan 23, 2014 3:09 am

Hi.
I'm trying to insert the google analytics JS code into the pages downloaded by CEF client.

I decided to use CefRegisterExtension() inside of ClientApp::OnWebKitInitialized() function.
Code: Select all
void ClientApp::OnWebKitInitialized() {
   
    // default code here...

    // my code:
    std::string js_code =
        "allowedDomain = 'some-domain.com';"
        "currDomain = window.location.host;"
        "if (currDomain === allowedDomain)"
        "{"
        "(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-12345678-1', currDomain);"
        "ga('send', 'pageview');"
         "}";

    bool bResult = CefRegisterExtension("google_analytics_extenstion", js_code,
        new ClientAppExtensionHandler(this));
}

But my application crashes every time it is trying to display the page.

If i chage the js_code to something like
Code: Select all
std::string js_code = "alert('bla')";
then application would not crash.

Is it something wrong with my JS code? It is valid JavaScript...
CantStop0708
Mentor
 
Posts: 60
Joined: Mon Jan 21, 2013 4:10 am

Re: CefRegisterExtension(). How to insert google analytics c

Postby magreenblatt » Thu Jan 23, 2014 9:10 am

You can't access the DOM from an extension. Use ExecuteJavaScript() instead.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: CefRegisterExtension(). How to insert google analytics c

Postby CantStop0708 » Thu Jan 30, 2014 6:24 am

I tried to ExecuteJavascript() with Google Analytics code inside of ClientHandler::OnLoadEnd() function.
Code: Select all
void ClientHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
                              CefRefPtr<CefFrame> frame,
                              int httpStatusCode)
{
   REQUIRE_UI_THREAD();

   if (m_BrowserId == browser->GetIdentifier() && frame->IsMain())
   {
      // We've just finished loading a page
      SetLoading(false);

      // Continue the DOM test.
      if (frame->GetURL() == dom_test::kTestUrl)
         dom_test::OnLoadEnd(browser);

      // added by me
      //---------------------------------------------------------------------------------------------------------------------
      // call Google Analytics code
      string js_GA_code =
         "(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-12345678-9', 'some-domain.com');"
         "ga('send', 'pageview');"
         ;
      frame->ExecuteJavaScript(js_GA_code, frame->GetURL(), 0);
      //---------------------------------------------------------------------------------------------------------------------
   }
}
But it doesn't seem like statistics is working. According to Gooogle Analytics i have zero site visits.
I have no idea what is wrong.
CantStop0708
Mentor
 
Posts: 60
Joined: Mon Jan 21, 2013 4:10 am

Re: CefRegisterExtension(). How to insert google analytics c

Postby CantStop0708 » Fri Jan 31, 2014 2:04 am

Is there a way i can insert JavaScript code into the page before rendering and displaying this page?
CantStop0708
Mentor
 
Posts: 60
Joined: Mon Jan 21, 2013 4:10 am

Re: CefRegisterExtension(). How to insert google analytics c

Postby magreenblatt » Fri Jan 31, 2014 10:16 am

CantStop0708 wrote:Is there a way i can insert JavaScript code into the page before rendering and displaying this page?

You can hook various DOM events using CefDOMVisitor in the renderer process (see tests/cefclient/dom_test.cpp for an example). If that isn't sufficient then you would need to intercept and rewrite the network response as described in viewtopic.php?f=6&t=10794.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: CefRegisterExtension(). How to insert google analytics c

Postby CantStop0708 » Tue Feb 04, 2014 3:12 am

magreenblatt wrote:You can use CefResourceHandler via CefRequestHandler::GetResourceHandler and execute the request/return the response contents yourself using CefURLRequest.

I probably didn't truly understand the meaning of this.
I spent whole day trying to intercept and rewrite the responce inside of ClientHandler::GetResourceHandler function but with no luck. :| Can i get some sample code please?
CantStop0708
Mentor
 
Posts: 60
Joined: Mon Jan 21, 2013 4:10 am

Re: CefRegisterExtension(). How to insert google analytics c

Postby CantStop0708 » Thu Feb 06, 2014 7:32 am

Seriously.
Even this few strings of code leads to the program crash.
Code: Select all
CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(
      CefRefPtr<CefBrowser> browser,
      CefRefPtr<CefFrame> frame,
      CefRefPtr<CefRequest> request)
{
   CefRefPtr<CefURLRequestClient> urlReqClient;
   CefRefPtr<CefURLRequest> urlRequest;
   urlRequest->Create(request, urlReqClient); // Assertion Failed! Expression: client.get()

   // default code here...
}
CantStop0708
Mentor
 
Posts: 60
Joined: Mon Jan 21, 2013 4:10 am

Re: CefRegisterExtension(). How to insert google analytics c

Postby magreenblatt » Thu Feb 06, 2014 9:54 am

You cannot use a NULL client. Also, static methods are called like CefURLRequest::Create.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: CefRegisterExtension(). How to insert google analytics c

Postby tmv » Thu Nov 20, 2014 2:13 am

Hello, CantStop0708.

Did you solve your problem? Is it finally real to add Google Analytics to CEF application?
tmv
Newbie
 
Posts: 1
Joined: Thu Nov 20, 2014 2:10 am

Re: CefRegisterExtension(). How to insert google analytics c

Postby GyaoG » Mon Jul 20, 2015 10:14 pm

Hello,I'm also trying to insert the google analytics JS code into the pages downloaded by CEF client.but I also don't know what to do ? Have you solved it?Can you say a litter of the solved idea?I'm looking forward to your response! Thanks you very much!
GyaoG
Mentor
 
Posts: 69
Joined: Mon Jul 20, 2015 9:56 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 47 guests