Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context #23

Closed
ianstormtaylor opened this issue Jul 15, 2014 · 7 comments
Closed

Context #23

ianstormtaylor opened this issue Jul 15, 2014 · 7 comments
Assignees

Comments

@ianstormtaylor
Copy link
Contributor

Forgot about the need for this one, but there's a field for all API calls to our API called context. And one of the things we do is that for each library we include library-specific information there. For Magento we'd want to include:

context: {
  library: {
    name: 'analytics-magento',
    version: '0.0.1'
  }
}

We use that internally for metrics and to do smart automating things in support and such. We'd want to add this automatically to all client-side and server-side calls from the extension

In analytics.js the third parameter to most of the calls is the context:

analytics.track('event', {/* properties */}, {
  library: {
    name: 'analytics-magento',
    version: '0.0.1'
  }
});

In PHP it takes a context key in the top-level array:

array(
  "userId" => "019mr8mf4r",
  "event" => "Purchased Item",
  "properties" => array(
    "revenue" => 39.95,
    "shipping" => "2-day"
  ),
  "context" => array(
    "library" => array(
      "name" => "analytics-magento",
      "version" => "0.0.1"
    )
  )
)
@astorm
Copy link
Contributor

astorm commented Jul 16, 2014

Adding this as a third parameter to the track calls.

analytics.track("Wishlisted Product",
segment_analytics_addedtowishlist.params,
{"library":{"name":"analytics-magento","version":"0.0.1"}}    );

I'm a little unclear how how this should be added to page/identity/alias -- especially page since a two parameter call to page is a category request?

window.analytics.page(segment_analytics_page.full_category_name, segment_analytics_page.page_title);      

I'm also a little unclear on why you included a PHP array above that has a top level key context after showing the syntax for track is to pass the context in as a third item.

@astorm astorm assigned ianstormtaylor and unassigned astorm Jul 16, 2014
astorm added a commit that referenced this issue Jul 16, 2014
astorm added a commit that referenced this issue Jul 16, 2014
@ianstormtaylor
Copy link
Contributor Author

Sure thing, we're actually just about to change the way this works for the Javascript library, so here's an updated version for the new API in Javascript only:

analytics.track('event', { property: 'something' }, {
  context: {
    library: {
      name: 'analytics-magento',
      version: '0.0.1'
    }
  }
});

The difference is that the options object is now nested (to provide for other potential API features other than context). This changed should be merged tomorrow or the next day, so we can make it now.

The other methods have their options object like so:

analytics.identify(userId, traits, options);
analytics.track(event, properties, options);
analytics.page(category, name, properties, options);
analytics.group(groupId, traits, options);
analytics.alias(userId, options);

So each time you see options, we should replace that with:

{
  context: {
    library: {
      name: 'analytics-magento',
      version: '0.0.1'
    }
  }
}

@ianstormtaylor
Copy link
Contributor Author

The PHP library (and other libraries) are structured slightly differently, since they are stateless. They have all of their parameters as keys in a top-level array, like so:

Analytics::track(array(
  "userId" => "user",
  "event" => "event",
  "properties" => array(),
  "context" => array(
    "library" => array(
      "name" => "analytics-magento",
      "version" => "0.0.1"
    )
  )
));

And all of the PHP calls have the same top-level structure, so they would all put context in an extra, top-level key.

Does that make sense? Let me know if you have questions.

@ianstormtaylor
Copy link
Contributor Author

Actually, I just realized, ignore the PHP stuff.

I think you're exposing a Magento model that wraps our PHP library? I just realized we shouldn't actually be exposing a proper PHP API, we should expose it in a way that just adds the calls they make there to the page, like we do with WordPress: https://github.com/segmentio/analytics-wordpress/blob/master/analytics-wordpress.php#L150

@astorm
Copy link
Contributor

astorm commented Jul 17, 2014

Ian thanks. One last question — what should I be passing in on the page calls that aren't categories? i.e., a regular page call looks like

analytics.page('Some Page');

Do I just do this?

analytics.page('Some Page',null,null,{...context...});

Or will that be interpreted as the category form

analytics.page(category, name, properties, options);

@ianstormtaylor
Copy link
Contributor Author

For those ones, the API will handle overloading for you if you want, so for example:

analytics.page(name, properties, options);

Works fine. But the nulls also work if that's clearer for you. Only reason properties is required there is because options is also an object, so we can't tell them apart.

Does that make sense?

astorm added a commit that referenced this issue Jul 29, 2014
astorm added a commit that referenced this issue Jul 29, 2014
@astorm
Copy link
Contributor

astorm commented Jul 29, 2014

Yup, makes sense. All calls (identify, track, page, alias, we're not using group) should have the context object now.

@astorm astorm closed this as completed Jul 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants