Using NewsCred Analytics with Dynamically Loaded Content

By default, the NewsCred analytics script will track an article when the page is loaded. If you'd like to track analytics for content that's added after the page loads, we expose two functions (trackPageview and stopTrackingPageview) to record your analytics.

1) window.ncAudienceInsights.trackPageview([options])

Records a pageview and begins recording engagement metrics for an article. The article must contain a NewsCred Analytics pixel to be tracked and a unique url must be provided for each article being tracked.

Parameters

options — an object containing two optional properties, node and url.

options.node — a DOM node that contains the NewsCred Analytics pixel img tag as a descendent. (See https://developer.mozilla.org/en-US/docs/Web/API/Element). If this is omitted, the script will look for a NewsCred Analytics pixel within document.body.

The NewsCred Analytics pixel is appended to the end of the HTML content of all NewsCred articles. This element can be present anywhere and at any level in the DOM node that is passed to trackPageview .The pixel HTML looks something like this:

<img src="https://pixel.newscred.com/px.gif?key=1234567890abcdef" alt="" height="1" width="1" class="nc_pixel" />

options.url — a URL (without the hostname) to use as the URL of your page.

Best Practice -- follow similar instructions to these from the Google Analytics Docs

Even if you don’t actually change the URL in the address bar, NewsCred Analytics will treat this as the URL at which the article lives. If this is omitted, the NewsCred Analytics will use the URL in the address bar. 

Usage

trackPageview will be called as soon as the DOM is ready, so if a pixel is present within the document body the pageview will be tracked then.

If an article is loaded dynamically after the page loads, you will need to manually call trackPageview. This will stop recording the engagement metrics for the previous article, record the pageview and start recording engagement metrics for the new article.

Articles won’t be tracked on trackPageview invocations if:

  • The passed DOM node has an invalid pixel or doesn’t have one at all.
  • The passed DOM node has multiple pixels that correspond to different articles.
  • A pageview was already tracked for a different article at the same URL.

 

var article = document.querySelector(‘article’);

window.ncAudienceInsights.trackPageview({

   node: article,

   url: ‘someUrl’

});

 

2) window.ncAudienceInsights.stopTrackingPageview()

Stops recording engagement metrics for the most recently tracked pageview. trackPageview calls this automatically before it begins recording engagement metrics for the new article, so in most cases you won’t need to call this manually.

Usage

window.ncAudienceInsights.stopTrackingPageview()

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk